Array of objects question

This is a discussion on Array of objects question within the C++ Programming forums, part of the General Programming Boards category; May you inform me what i should do so that the following: Code: #include <iostream> class class_a {public: int x; ...

  1. #16
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    May you inform me what i should do so that the following:
    Code:
    #include <iostream>
    class class_a
    {public:
    
    	int x;
    	int func(){return 1;}
    };
    
    class class_b:public class_a
    {public:
    
    int func(){return 2;}
    	int x;
    };
    int main()
    {
    class_a **table=new class_a*[5];
    class_b *node=new class_b();
    table[0]=node;
    int x=table[0]->func();
    std::cout<<x;
    system("pause");
    return 0;
    }
    prints "2" instead of "1"?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    func() should be a virtual function. You should also have virtual destructors for both classes. You should also delete/delete[] properly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Does this work even when there is only a declaration (extern class_a ** table)
    in the file that i call the function?

    -Silly post forget about it .. -
    Last edited by tezcatlipooca; 01-03-2007 at 09:59 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. A question about dynamically creating an array of objects
    By edd1986 in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2006, 11:30 AM
  3. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. for loop to create an array of objects
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 05-03-2002, 06:26 PM

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