1
class game_list
{
    public:
        string name;
        float price;
        string platform;
        string console;
        string condition;
        bool is_portable;
        string N_bits;

};

class catridge_object:public game_list
{
    string N_bits;
    bool is_import;
};

class disk_object:public game_list
{
    string disk_type;
    int n_disk;
};

class digital_object:public game_list
{
    float file_size;
    bool is_for_pc;
};

game_list *pointerMain;

int main()
{
    int optionChosen=0;
    vector<game_list*> mainVector;

}

here game_list is the parent class and there are child classes derived from it. I am trying to create a list of game_class objects that the vector mainVector will hold. Now, depending on user input, all the objects inside the vector will have the common attributes that are described in game_list class and also depending on what user chooses, it will have an additional info from the 3 other child classes derived from the parent class. So I will be creating a dynamic game_list using the following command

pointerMain=new game_list;

Everything seems ok but the problem is when I try to access the child class using pointerMain->(any member of the child class), it doesnt work that way. Any ideas on what to do?

  • If you use `new game_list` then it doesn't have members defined by a child class, it's only a the parent class. – Barmar May 18 '13 at 09:31
  • @Barmar but doesnt `new game_list` dynamically creates the child class too? –  May 18 '13 at 09:32
  • 1
    @user2086751 Why should it? You'd have to do something like `new cartridge_object`. These are extreme basics; I think you'd be better off starting with a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Angew is no longer proud of SO May 18 '13 at 09:34
  • No. Creating a child class includes the base class, not the other way around. – Barmar May 18 '13 at 09:34
  • 1
    You have 3 child classes, which one do you expect it to make? – Barmar May 18 '13 at 09:34
  • @Barmar it depends on the user input which one will be created. Its one or the other. –  May 18 '13 at 09:45
  • 1
    Are you using the Factory pattern in the `game_list` constructor? There's nothing like that in the code you showed. – Barmar May 18 '13 at 09:47
  • @Barmar I dont have to worry about constructor/destructors. Another Idea I am having is that putting a pointer inside the `game_list` class which will point to the other child classes. Any ideas about that? –  May 18 '13 at 09:53
  • "it depends on the user input which one will be created. Its one or the other" — then show the code you are using to create them; if you don't all we can do is make guesses about the code you *might* have for object creation. – msw May 19 '13 at 06:05

3 Answers3

1

You need cast pointerMain to the derived class type.

catridge_object* obj = dynamic_cast<catridge_object*>(pointerMain);
if (obj)
  obj->members_of_catridge_object...

Also, the base class should be polymorphic, so you need add at least a virtual method in the base class (the best choice is destructor member).

 

or (for certain casts):

static_cast<catridge_object*>(pointerMain)->members_of_catridge_object...;

Be careful it works if pointerMain made by new catridge_object, otherwise the behavior is undefined.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • This won't work, the classes don't have any `virtual` members, so they don't have RTTI. Also, you should always check the result of `dynamic_cast` for null value before using it. If you're *certain* the object is of the correct dynamic type, you can as well use `static_cast`. – Angew is no longer proud of SO May 18 '13 at 09:38
  • With this edit, there is no longer any risk of UB. You could also mention that the "at least one virtual member function" should be the destructor :-) – Angew is no longer proud of SO May 18 '13 at 09:42
0

Not every child class will have the same members so to access them you will need to know the actual type. Either add a virtual destructor to the base class and use dynamic_cast or put the functionality that needs those attributes into a virtual function (declared in the base class and implemented by the children)

And as others have pointed out new game_list will create an object of type game_list and not of one of the child classes.

If you do choose to go the RTTI (dynamic_cast) route, be sure to check that it returns a non-Null pointer (which it will if the cast-to type does not match)

Joe
  • 6,497
  • 4
  • 29
  • 55
0

It is possible to cast a base pointer into a pointer of the derived type to access a member of the derived class through the base pointer. For example, the following code is valid in C++,

((disk_object *)pointerMain)->n_disk=10;
Deepu
  • 7,592
  • 4
  • 25
  • 47