Loosing inherted data members. Trying Downcasting.

This is a discussion on Loosing inherted data members. Trying Downcasting. within the C++ Programming forums, part of the General Programming Boards category; I am having trouble with I believe Downcasting. There are two classes, a base class and an inherited class. What ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Loosing inherted data members. Trying Downcasting.

    I am having trouble with I believe Downcasting.

    There are two classes, a base class and an inherited class.

    What I'm trying to do is store refernces to the two mixed classes within a single list

    but when I push the reference to the object into the list, it looses it's non base class datamembers

    I believe I need to Downcast, and so am trying to , but am still loosing the datamembers

    Base Class:


    Code:
    class Vehicle
    {
    public:	
    	Vehicle(int publicmpg = 0); 
    
    protected:
    	int privatempg;  
    };
    
    Vehicle::Vehicle(int publicmpg) 
    {
    	privatempg = publicmpg; 	
    }

    Derivied Class:


    Code:
    class SportsCar : public Vehicle
    {
    public:
    	SportsCar(int publicsixspeed = 0, int publicmpg = 0);
    
    protected:
    	int protectedsixspeed;
    };
    
    SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg) 
    {
    	protectedsixspeed = publicsixspeed;
    }
    main:


    Code:
    #include <iostream>
    #include <list>
    #include "Vehicle.h"
    
    using namespace std;
    
    int main()
    {
    	list<Vehicle> listname;
    
    	int publicmpg = 0;
    	int publicsixspeed = 0;
    	int publicelectricroof = 0;
    
    	SportsCar node(publicmpg, publicsixspeed);
    
    	SportsCar *ptrToSportsCar;
    
    	//attempting to correct the problem by Downcasting
    	ptrToSportsCar = dynamic_cast<Vehicle*>(&node);
    
    	//Push the SportsCar object onto the list, but inhertited datamember 'protectedsixspeed' is lost.
    	listname.push_back( *ptrToSportsCar );
    
    	system ("PAUSE");
    	return 1;
    }


    Visual studio reports:


    cannot convert from 'Vehicle *' to 'SportsCar *'
    I've just moved house and stuck on dialup, so have read the book I have
    on the subject, and this is what I have come up with after reading it.

    It compiles and is what I can gather from my book
    as to be correct, but seems is not right.

    Could anyone provide a solution at all?

    It would be most appreciated.

    Thank you !

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,795
    The problem is that you are storing Vehicle objects in the std::list. Consequently, trying to store SportsCar objects in the std::list results in type slicing, where the objected stored really is just a Vehicle, not a SportsCar, hence attempts to cast it back to a SportsCar will fail.

    Polymorphism in C++ works via pointers and references, so the solution is to use a container of pointers instead. This can mean using a std::list<Vehicle*>, a std::list<std::tr1::shared_ptr<Vehicle> >, or if you have access to Boost, a boost::ptr_list<Vehicle>.

    Incidentally, it probably makes no sense to actually have Vehicle objects. The Vehicle class should probably be an abstract base class. Whether it is an abstract base class or a concrete class that is also a base class, the fact that it is a base class means that you should declare its destructor virtual since you may attempt to destroy a derived class object through a base class pointer.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Using classes as data members
    By wjday in forum C++ Programming
    Replies: 1
    Last Post: 08-01-2002, 10:42 PM
  4. static and non-static data members
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2002, 10:06 AM
  5. Value of data members
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 06-14-2002, 10:20 AM

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