Problem with Downcasting

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 3
Reputation: Greeny28 is an unknown quantity at this point 
Solved Threads: 0
Greeny28 Greeny28 is offline Offline
Newbie Poster

Problem with Downcasting

 
0
  #1
May 11th, 2009
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:

  1. class Vehicle
  2. {
  3. public:
  4. Vehicle(int publicmpg = 0);
  5.  
  6. protected:
  7. int privatempg;
  8. };
  9.  
  10. Vehicle::Vehicle(int publicmpg)
  11. {
  12. privatempg = publicmpg;
  13. }
  14.  
  15. //---------------------------------
  16.  
  17. class SportsCar : public Vehicle
  18. {
  19. public:
  20. SportsCar(int publicsixspeed = 0, int publicmpg = 0);
  21.  
  22. protected:
  23. int protectedsixspeed;
  24. };
  25.  
  26. SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg)
  27. {
  28. protectedsixspeed = publicsixspeed;
  29. }

and here is main.cpp:

  1. #include <iostream>
  2. #include <list>
  3. #include "Vehicle.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. list<Vehicle> listname;
  10.  
  11. int publicmpg = 0;
  12. int publicsixspeed = 0;
  13. int publicelectricroof = 0;
  14.  
  15. SportsCar node(publicmpg, publicsixspeed);
  16.  
  17. SportsCar *ptrToSportsCar;
  18.  
  19. ptrToSportsCar = &node;
  20.  
  21. listname.push_back( *ptrToSportsCar );
  22.  
  23. system ("PAUSE");
  24. return 1;
  25. }

I know I should change:
  1. list<Vehicle> listname;

to:

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with Downcasting

 
2
  #2
May 11th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Problem with Downcasting

 
0
  #3
May 11th, 2009
>>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 );
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: Greeny28 is an unknown quantity at this point 
Solved Threads: 0
Greeny28 Greeny28 is offline Offline
Newbie Poster

Re: Problem with Downcasting

 
0
  #4
May 11th, 2009
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!!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: Greeny28 is an unknown quantity at this point 
Solved Threads: 0
Greeny28 Greeny28 is offline Offline
Newbie Poster

Re: Problem with Downcasting

 
0
  #5
May 11th, 2009
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!

Attached Thumbnails
SP32.jpg  
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Problem with Downcasting

 
0
  #6
May 11th, 2009
>>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]));
Last edited by siddhant3s; May 11th, 2009 at 11:45 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with Downcasting

 
2
  #7
May 11th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC