| | |
Problem with Downcasting
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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:
and here is main.cpp:
I know I should change:
to:
And when I do Visual Studio 2005 reports:
Could anyone show me what else I need to do to get this working?
many thanks for any help1
greeny
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
list<Vehicle> listname;
to:
C++ Syntax (Toggle Plain Text)
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
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.
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.
>>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
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
(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
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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!!
(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!!
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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!
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!
>>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:
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
(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
>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.
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.
![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Cellphones, PDAs and Handheld Devices)
- Connection Problems (Networking Hardware Configuration)
- Encoding (Unicode) problem in IE 6.0 (Web Browsers)
- .htaccess mod_rewrite problem (Linux Servers and Apache)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: Day Of Year Problem
- Next Thread: rand() (not really random)
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






