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

};

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

game list is dynamically created and a pointer called mainPointer points to that dynamic object. But I am confused on how to access cartridgeClasss elements through the parent class game_list. I already tried mainPointer->N_bits. Seems like it doesnt work that way? Sorry I am just a noob at c++.

  • You need to cast the pointer to the derived class – Nick May 17 '13 at 21:18
  • Even needing this indicates some kind of major design flaw. Think your code through again to see whether you can avoid it. – antonijn May 17 '13 at 21:27
  • @antonijn Needing this is the main objective of the polimorphism. I don't get your point. – Ian Medeiros May 17 '13 at 21:30
  • I have to have a single pointer pointing to `game_list` and depending I will have a bunch of these pointers inside a `vector`. Some of these pointers will have the child class and some wont. So is casting appropriate for this scenario? –  May 17 '13 at 21:32
  • 1
    it's possible, yes, but it's not the way I'd do it. You'll need a way to check whether a `game_list` is a `catridgeClass`. – antonijn May 17 '13 at 21:35
  • Can you edit your question to explain what actually you are trying to do? That way we can give you a more precise answer. – Ian Medeiros May 17 '13 at 21:38
  • 1
    Not trying to take you down, but I really recommend you to take a good c++ book and read the chapter about inheritance. You probably lack basic understandings of what is possible to accomplish with it. – Ian Medeiros May 17 '13 at 21:50

4 Answers4

2

To access catridgeClass attributes, you will need a catridgeClass object.

First, you need to correct your class to have public attributes.

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

class anotherClass: public game_list
{
   public:
      string other_member;
};

Than you need to get a catridgeClass object from the caller function:

int main()
{
   std::vector<game_list *> games;

   games.push_back(new catridgeClass);
   games.push_back(new anotherClass);

   for(int i=0; i<games.size(); i++)
   {
      //You will need to get a catridgeClass object to access it's members.
      catridgeClass *catridge_ptr = dynamic_cast<catridgeClass *>(games[i]); 

      //Or to access anotherClass.
      anotherClass *another_ptr = dynamic_cast<anotherClass*>(games[i]);

      //If the conversion is not possible, dynamic_cast will return a null pointer
      if(catridgeClass != NULL)
        std::cout << catridge->N_bits << std::endln;
      if(anotherClass  != NULL)
        std::cout << anotherClass->other_member << std::endln;
   }

   //And of course, you need to avoid a memory leak
   for(int i=0; i<games.size(); i++)
      delete games[i]

   return 0;
}

Keep in mind that it's not a desired design. If you are trying to use polimorphism, is because all that classes share common behaviours. A better approach would be to use interfaces.

Ian Medeiros
  • 1,746
  • 9
  • 24
  • `game_list` will be a dynamically created class which will be pointed to by a pointer and some of these dynamically created classes will have the child classes attributes and some wont. All the pointers pointing to `game_class` will be stored in a vector. And I need to access the child classes attributes using the pointer that points to `game_list`. Sorry if I am confusing you. –  May 17 '13 at 21:52
  • @user2086751 Edit your question and try YOUR BEST to explain what you are trying to do. Or just get a good c++ book and read the chapter about inheritance. – Ian Medeiros May 17 '13 at 21:54
1

Try a virtual method getNBits() = 0; in parent class, and define it in child class.

See: C++ Virtual/Pure Virtual Explained

Community
  • 1
  • 1
yoones
  • 2,394
  • 1
  • 16
  • 20
  • 1
    With this you should remember that you cannot instantiate parent class. – Losiowaty May 17 '13 at 21:20
  • 1
    Not to mention, you've saddled the base class with knowledge of the derived class, and in a complex way. E.g., N_bits could just be declared in the base with a special default value, like 0. – Scott Jones May 17 '13 at 21:23
1

A parent class has no information about its child class. The only way to do what you want is to either cast like Dory suggested -- which I usually frown upon -- or to create a virtual function in the parent class that the child class redefines.

Why do I frown upon the cast? Most of the time dynamically casting an object to get data from it represents poorly written code. I'm not sure what the rest of your code looks like, but my guess is that there's a better way to write this via shared functionality. For example, let's say what you want to use these items for is displaying string information. In such a case, the best way to write it would be using virtual functions:

class game_list
{
    public:
        string name;
        float price;
        string platform;
        string console;
        string conditin;
        bool is_portable;
        public virtual void PrintInfo() 
        { 
            cout << "name: " << name << ", price: " << price; //etc
        } 

};

class catridgeClass:public game_list
{
    string N_bits;
    bool is_import;
    public virtual void PrintInfo()
    {
        game_list::PrintInfo();
        cout << ", bits: " << bits << ", is import: " << is_import;
    }
};

Now calling mainPointer->PrintInfo() will print the correct information regardless of its underlying type. Much nicer, and it's the "Object-Oriented Way."

  • Completely agree with you on the casting vs virtual functions here. I'll upvote it just for pointing out that virtual methods are probably a better solution, since the op has given us very few details about his/her problem. – antonijn May 17 '13 at 21:40
0

try to use dynamic_cast

catridgeClassPtr = dynamic_cast<catridgeClass*>(GameListPtr);

catridgeClassPtr->N_bits;

full info on dynamic_cast: http://www.cplusplus.com/doc/tutorial/typecasting/

Scott Jones
  • 2,880
  • 13
  • 19
Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
  • but I have to have only one pointer pointing to the parent class from which I can access the attributes of both the parent and child classes. Because all these pointers will be inside a vector. –  May 17 '13 at 21:24