Polymorphism question

This is a discussion on Polymorphism question within the C++ Programming forums, part of the General Programming Boards category; Hi, I have an array of base class pointers which I initialize with derived class pointers. However when I call ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    Polymorphism question

    Hi,

    I have an array of base class pointers which I initialize with derived class pointers. However when I call the Run() function, only the base class one gets called.

    Heres my code -
    Code:
    void SomeFunction()
    {
    	Car** carArray =  (Car**)new SportsCar*[100];
    	for(int i = 0; i < 100; ++i)
    		carArray [i] = new SportsCar();	
    	CreateCars(carArray ,100);
    }
      
    void CreateCars(Car** carArray, int num)
    {
           Car** car = carArray;
    	for(int i = 0; i < num; ++i)
    	{
    		car [i]->SetSpeed(rand()%100);
    	}
    	carDriver = new carDriver(car,num);
    }
         class Car
        {
            ...
           virtual void Run() { cout << "CAR" << endl; } 
         };
    
         class SportsCar 
           :public Car
         {
             void Run() { cout << "SPORTS CAR" << endl; } 
          };

    All I get is a "CAR", any Ideas ? also, my Car class has a few pointers..do I need to define a copy constructor/assignment operator based on the code I posted above ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If your Car class has pointers, you probably need a copy constructor, yes. I don't see where you are calling Run from in this code, though.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Ahh, I found the bug.. I had another array on which Run was called which held only static cars. That is why it wasnt working properly.

    For the copy constructor, My car class has pointers to other classes such as Engine which have further pointers and so on. So how am I supposed to go about this ? Can someone show me an example or point me to a tutorial! anything will do..Thanks!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you want them to point at the same engine object, or should each have their own engine? If the first you can just copy the pointers. If the second, you need to make a new engine that is a copy of the other, and set the new pointer appropriately.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Does your class delete the pointers in the destructor? If it does, that's a good hint that you need a copy constructor (and copy assignment operator) or that you need to disable them by declaring them as private and not implementing them.

    If you don't have a destructor or it doesn't delete the pointers, then you probably don't need an explicit copy constructor or copy assignment operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. Polymorphism newbie question
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:38 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. A question Polymorphism
    By omeydhomey in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2003, 10:28 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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