Thread: Array of objects question

  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
    28,413
    func() should be a virtual function. You should also have virtual destructors for both classes. You should also delete/delete[] properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 10:59 AM.

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, 12:30 PM
  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