Hi,

I was hoping someone could correct my mistake.

I'm trying to store references (or pointers should that be better) in a hetrogenous List.

(The references or pointers will point to mixed classes).

The problem is that everytime I store the pointer which holds a reference to the object, the non base class datamembers are being lost.

I have done some research, but am just not getting it, so if someone could tell me how to correct it I would be most appreciated.

I've spent ages on this and am getting nowhere, and don't have broadband at home. I've read my book though.

Here are the two classes:

class Vehicle
{
public:	
	Vehicle(int publicmpg = 0); 

protected:
	int privatempg;  
};

Vehicle::Vehicle(int publicmpg) 
{
	privatempg = publicmpg; 	
}

//---------------------------------

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;
}

and here is main.cpp:

#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;

	ptrToSportsCar = &node;

	listname.push_back( *ptrToSportsCar );

	system ("PAUSE");
	return 1;
}

I know I should change:

list<Vehicle> listname;

to:

list<Vehicle*> listname;

And when I do Visual Studio 2005 reports:

error C2664: 'std::list<_Ty>::push_back' : cannot convert parameter 1 from 'SportsCar' to 'Vehicle *const &'
with
[
_Ty=Vehicle *
]
Reason: cannot convert from 'SportsCar' to 'Vehicle *const '
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Could anyone show me what else I need to do to get this working?

many thanks for any help1

greeny :)

>And when I do Visual Studio 2005 reports:
That's because you changed only half of the equation. In the code as posted, you create a list of Vehicle objects and populate it with the Vehicle half of SportsCar objects (an effect called slicing). When you changed the list to take pointers to Vehicle, you continued to pass SportsCar objects rather than pointers to SportsCar. You need to change your push_back to take a pointer.

>return 1;
Note that a non-zero return from main denotes failure.

>>And when I do Visual Studio 2005 reports:
Thats because you are still appending a SportsCar in the list while it requires a Pointer to SportsCar.
So change listname.push_back( *ptrToSportsCar ); to listname.push_back( ptrToSportsCar );

Many thanks for the help guys. I will run back home and try this out.

(Just moved house so have no internet connection right now).

If anyone else any input I would be most gratefull.

Will try to get back before the libary closes and post if it has worked.

honestly, realy appreciative of your help.

Thank you!!

Ok I've been home and tried it.

Compiles fine, I have attatched a screen shot of the IDE as I'm concerned it still isn't storing correctly.

Can anyone confirm that it is working correctly?

I'm worried as element 0 in the list isn't showing the datamember protectedsixspeed.

Really appreciate the help.

Thanks!

:)

Attachments SP32.jpg 62.75 KB

>>Can anyone confirm that it is working correctly?
What do you mean by working correctly?
I don't think there is any problem with the code now.
If you are pointing that your debugger is not showing protectedsixspeed for listname[0], if should not. As listname[o] contains the pointer to a Vehical Object. You know it is actually a SportsCar. To get a SportsCar object pointed by listname[0], consider doing a static_cast: SportsCar sp=*(static_cast<SportsCar*>(listname[0]));

>Can anyone confirm that it is working correctly?
It's working correctly, but probably not as you intended. If you look at a SportsCar through Vehicle-colored glasses, you'll only see a Vehicle. If you're absolutely sure that the Vehicle is actually a SportsCar, you can cast it to the correct pointer type and see a SportsCar[1].

The "correct" object oriented way to manage this problem is to support an interface that you intend to use on all children of Vehicle, and not try to use polymorphism unless you only work through that interface.

[1] Or you can make a big mess if the Vehicle isn't actually a SportsCar.

This article has been dead for over six months. Start a new discussion instead.