CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Am I no longer Downcasting?

  1. #1
    Join Date
    Mar 2008
    Posts
    31

    Am I no longer Downcasting?

    Hi,

    I am starting to write a program which will store pointers to mixed classes within a single vector, but have been having trouble with Downcasting and the inherited classes datamembers being sliced off.


    I was hoping someone could take a look and see if I've now fixed the doncasting occuring.

    Here is the code I am using:

    vehicle.h :

    Code:
    class Vehicle
    {
    friend class Vectorclass;
    
    public:    
        Vehicle(int publicmpg = 0);
    
    protected:
    
    
        int protectedmpg;  
    };
    
    Vehicle::Vehicle(int publicmpg)
    {
        protectedmpg = publicmpg;     
    }
    
    //-----------------------------------------------------
    
    class Vectorclass
    {
    public:
        
        //addvehicle()
        //deletevehicle()
    
    protected:
    
        //Vehicle *first;    
    };
    
    //------------------------------------------------------
    
    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.cpp :

    Code:
    #include <iostream>
    #include <vector>
    #include "Vehicle.h"
    
    //This program pushes a pointer of type Vehicle into the vector, but the pointer stored
    //actually points to a type SportsCar (hence why listname[0] doesn't show the inherited 
    //datamember 'protectedsixspeed' as the vector is declared to use type Vehicle?).
    
    //I'm thinking this as both the object and the pointer to the object are 
    //complete with the inherited datamember.
    
    using namespace std;
    
    int main()
    {
    	vector<Vehicle*> vectorname;
    
        int publicmpg = 11;
        int publicsixspeed = 22;
    
        SportsCar node(publicmpg, publicsixspeed);
    
        SportsCar *ptrToSportsCar;
    
        ptrToSportsCar = &node;
    
        vectorname.push_back( ptrToSportsCar );
    
        cout << "past push" << endl;
    
        system ("PAUSE");
        return 0;
    }



    He are two screen shots of my IDE after the pointer has been pushed into the vector:


    Autos:




    Locals:



    Could anyone confirm that the Downcasting is no longer occuring please?

    Many thanks for any help!


  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: Am I no longer Downcasting?

    By using a vector<Vehicle*> instead of a vector<Vehicle>, you avoid the slicing problem that you are worried about, so you are probably on the right track. However, Vehicle is not a polymorphic base class in your example since it has no virtual functions. It definitely should have a virtual destructor, and it probably should be an abstract base class.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2008
    Posts
    31

    Re: Am I no longer Downcasting?

    Thanks laserlight!

    You must spread some Reputation around before giving it to laserlight again.
    I have not yet added a virtual destructor, but...


    I have created Vehicle to now be an Abstract Base Class, but originally I was going to use the class Vectorclass for declaring the pure virtual functions,

    Can I totally get rid of the Vectorclass class?

    Is it's being there pointless?

    The making Vehicle the ABC seems to be working fine.

    Cheers!

    New header file:
    Code:
    using namespace std;
    
    class Vehicle
    {
    	friend class Vectorclass;
    
    public:	
    	Vehicle(int publicmpg = 0); 
    
    
    	//Declaring pure virtual function here causing Vehicle to become the ABC
    	virtual void myfunc() = 0; ///////////////////////////////////////////////////////////////
    
    
    protected:
    
    	int protectedmpg;  
    };
    
    Vehicle::Vehicle(int publicmpg) 
    {
    	protectedmpg = publicmpg; 	
    }
    
    //-------------------------
    //
    //-------------------------
    
    class Vectorclass
    {
    public:
    
    	//was going to declare pure virtual member functions here originally ///////////////////////
    	// ie: AddVehicle()/////////////////////////////////////////////////////////////////////////////
    
    	Vectorclass(int publicfirst = 0);
    
    protected:
    
    	int protectedfirst; 
    };
    
    Vectorclass::Vectorclass(int publicfirst)
    {
    	protectedfirst = publicfirst;
    }
    
    //-------------------------
    //
    //-------------------------
    
    class SportsCar : public Vehicle
    {
    public:
    	SportsCar(int publicsixspeed = 0, int publicmpg = 0);
    
    	//pure virtual member function for SportsCar /////////////////////////////////////////////////////////
    	void myfunc()
    	{
    		cout << "Inside myfunc() for SportsCar objects" << endl;
    	} 
    
    
    protected:
    	int protectedsixspeed;
    };
    
    SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg) 
    {
    	protectedsixspeed = publicsixspeed;
    }
    New main.cpp:
    Code:
    #include <iostream>
    #include <vector>
    #include "Vehicle.h"
    
    using namespace std;
    
    int main()
    {
    	vector<Vehicle*> vectorname;
    
    	int publicmpg = 11;
    	int publicsixspeed = 22;
    
    	SportsCar node(publicmpg, publicsixspeed);
    
    	SportsCar *ptrToSportsCar;
    
    	ptrToSportsCar = &node;
    
    	vectorname.push_back( ptrToSportsCar );/////////////////////////////////////////////////
    
    	cout << "past push" << endl;
    
    
    	vectorname[0]->myfunc(); 
    	
    
    	cout << "Past myfunc() call " << endl;
    
    	system ("PAUSE");
    	return 0;
    }
    Last edited by Swerve; May 29th, 2009 at 03:11 PM.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: Am I no longer Downcasting?

    Quote Originally Posted by Swerve
    Can I totally get rid of the Vectorclass class?

    Is it's being there pointless?
    If you want to use Vectorclass as a Vehicle container then that is fine, but you should provide a suitable interface and use a more descriptive name. This could come in handy if say, you cannot use std::vector<std::tr1::shared_ptr<Vehicle> > or boost::ptr_vector<Vehicle>, in which case you could wrap a std::vector<Vehicle*> with this Vehicle container and then implement the container's copy constructor, copy assignment operator and destructor to perform RAII (i.e., when the container is copied/destroyed, not only is its std::vector<Vehicle*> member variable copied/destroyed, but the objects that the pointers point to are also copied/destroyed).

    Quote Originally Posted by Swerve
    I have created Vehicle to now be an Abstract Base Class, but originally I was going to use the class Vectorclass for declaring the pure virtual functions
    This container is unlikely to be appropriate as a base class, hence it should not have virtual functions at all. It looks like you want Vehicle to be a polymorphic base class, hence it is the one that should have at least one virtual function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Mar 2008
    Posts
    31

    Re: Am I no longer Downcasting?

    Thank you laserlight.

    I've never created a 'container class' but reading how it would allow me to use shared_ptr is something I may go on to need I think ( due to having mixed classes? ), so for now will leave 'vectorclass' commented out

    Now I'll create all the pure virtual functions from ABC Vehicle, there functionalities will be AddVehicle(), DeleteVehicle() and SearchForVehicle().

    Then I'm guesing I may have to use the 'containerclass' you describe to point to the different type objects with the shared_ptr,

    I was TBH thinking I could of used the Vehicle class for all the functionality, but if I'm not mistaken, the 'container class' would give me an Interface, which is what I'd like to have, to eventually create a GUI for the program for example.

    Any advice/thoughts from anyone are much valued.

    Thanks


    EDIT - Sweet, seems you cannot create instances of an Abstract base class, so will go back to using the 'vectorclass', give it an appropiate name, and use this as the interface (is there a naming convention for interfaces? ).

    Programs starting to take shape now
    Last edited by Swerve; May 30th, 2009 at 10:10 AM.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: Am I no longer Downcasting?

    Quote Originally Posted by Swerve
    Now I'll create all the pure virtual functions from ABC Vehicle, there functionalities will be AddVehicle(), DeleteVehicle() and SearchForVehicle().
    That does not make sense since those member functions sound more like they belong to a Vehicle container than to a Vehicle.

    Quote Originally Posted by Swerve
    I was TBH thinking I could of used the Vehicle class for all the functionality, but if I'm not mistaken, the 'container class' would give me an Interface, which is what I'd like to have, to eventually create a GUI for the program for example.
    The Vehicle class and its derived classes should each provide a well defined, minimal but complete, interface as well.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)