I want an array of pointers where each member can point to a different class

This is a discussion on I want an array of pointers where each member can point to a different class within the C++ Programming forums, part of the General Programming Boards category; Lets say i have 3 classes defined. cMyClassA cMyClassB cMyClassC and then this in the code. Code: cMyClassA* A = ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    I want an array of pointers where each member can point to a different class

    Lets say i have 3 classes defined.

    cMyClassA
    cMyClassB
    cMyClassC

    and then this in the code.

    Code:
    cMyClassA* A  = new cMyClassA;
    cMyClassB* B = new cMyClassB;
    cMyClassC* C = new cMyClassC;
    What I want is a single array of pointers where the first elemet can point to A, the second to B, and the third to C.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I suspect that this is the kind of thing you're looking for based on your other thread.
    Code:
    class BaseClass
       {
       public:
          virtual ~BaseClass(){}=0;
          virtual void DoSomething()=0;
       };
    
    class A : public BaseClass
       {
       public:
          void DoSomething()
             {
             //Do something version A
             }
       };
    
    class B : public BaseClass
       {
       public:
          void DoSomething()
             {
             //Do something version B
             }
       };
    
    class C : public BaseClass
       {
       public:
          void DoSomething()
             {
             //Do something version C
             }
       };
    
    int main(void)
       {
       BaseClass *list[3];
       list[0] = new A;
       list[1] = new B;
       list[2] = new C;
       
       for(int i=0; i < 3; i++)
          list[i]->DoSomething();
       for(i=0; i < 3; i++)
          delete list[i];
       return 1;
       }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    thanks alot man, it seems to work great. Dose using the virtual function in this manner represent good coding techniques?

    The code wouldnt compile with the virtual deconstructor, I cant really see why its needed...

    Could someone explain what exactly is happening when you do the inheretance thing? Ive never used that before but ive seen it in many places.

    class A : public BaseClass

    And whats the difference in the virtual and not having the virtual? Ive never used it so I lack the vocabulary to understand exactly whats happeneing in the code.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    virtual destructors are necessary when you plan on deleting from a base class pointer. Otherwise deconstruction of the derived class will not take place. I don't know why it wouldn't compile. What compiler?

    Good coding technique? Sure, definitely. It's one of the most powerful features of the language. But the architecture depends on a lot more than that.

    virtual is a function that can be "unknown" by the base class. Essentially, when a function is virtual, it is really a function pointer stored in the class. The function pointer is filled in when the derived class constructor is called. This allows the base class to call a derived class's method even when he doesn't know anything about the implementation.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You can make a pure virtual destructor, but you also MUST provide a definition for the destructor. This is not only legal but required. All derived destructors will ultimately call the base destructor, and it will crash if there is no definition.

    This technique (pure virtual destructor) is useful if you want to make an abstract base class that has no other pure virtual functions, but you can never get around the need to have a defined destructor for any base class, even IF it is pure virtual.

    This:

    virtual ~BaseClass(){}=0;

    is also not legal because of the {}.

    You need to have:
    virtual ~BaseClass()=0;

    And elsewhere:

    BaseClass::~BaseClass(){}
    Last edited by Cat; 09-10-2003 at 09:12 PM.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    interesting. I don't usually compile things before I post them as answers. I thought I remember doing it that way before. In reality I don't usually make them pure. There usually isn't much of a point because they live in a class with other pure virtual functions.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class warfare
    By disruptivetech in forum C++ Programming
    Replies: 13
    Last Post: 04-22-2008, 02:43 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21