![]() |
| | #16 |
| Registered User Join Date: Jul 2009
Posts: 14
| Thanks again so much for being patient with me, I know I can be pretty hard to explain things to. |
| Aaronugio is offline | |
| | #17 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| BTW, in real world code, you'd want to check the return value of dynamic_cast (when used with pointers) to see if it's NULL, which means the pointer isn't the right type; or put it in a try/catch block around a dynamic_cast of a reference and catch a bad_cast exception, which also means the reference isn't the right type. Code: Animal* a = new Cat;
Dog* p = dynamic_cast<Dog*>( a );
if ( p == NULL )
{
cout << "Oops, I guess 'a' isn't really a Dog!" << endl;
}
Animal& b = *a;
try
{
Dog& r = dynamic_cast<Dog&>( b );
}
catch ( std::bad_cast& e )
{
cout << "Oops, I guess 'b' isn't really a Dog!" << endl;
}
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
| | #18 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| Your destructor's should be virtual.
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #19 |
| Registered User Join Date: Jul 2009
Posts: 14
| Thanks guys for all the help, I have one last question on the subject, let's say i have Dog, object, but want it to behave like a Cat object, assuming they're both on the same hierarchy level, would it be more efficient to use casting at all, or just destroy the Dog, and recreate the object as a Cat, and go with it from there? |
| Aaronugio is offline | |
| | #20 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,353
| Quote:
__________________ 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 | |
| laserlight is online now | |
| | #21 |
| Registered User Join Date: Jul 2009
Posts: 14
| well it's not so much that i want the Dog to BE a cat so much as i want it to be able to perform a couple of Cat functions or would it be more effective to just write the two or so Cat functions as protected functions within the parent Mammal Class |
| Aaronugio is offline | |
| | #22 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,353
| Quote:
__________________ 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 | |
| laserlight is online now | |
| | #23 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Why would a Dog need Cat functions? If it needs that, then the design should be flawed. You should try to keep functionality that both classes need in a common base class, but it should also be something one can expect from that class. Like a Dog not meowing.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #24 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| So basically, you want a Dog that's afraid of water and says "Meow"?
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
| | #25 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> So basically, you want a Dog that's afraid of water and says "Meow"? Sort of like how Mr. Ed can talk like a human, neigh? |
| Sebastiani is offline | |
![]() |
| Tags |
| casting, classes, problem, vc++ |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Advantages of c++ type casting over c type casting | kaibalya2008 | C++ Programming | 10 | 05-05-2009 11:09 AM |
| Casting Question (I think) | fayte | C Programming | 6 | 03-08-2006 05:31 PM |
| casting the system exstracted date into seperate ints | bazzano | C Programming | 1 | 08-30-2005 12:17 AM |
| Type casting | Lionmane | C Programming | 28 | 08-20-2005 02:16 PM |
| question about casting pointers/other types also?? | newbie02 | C++ Programming | 3 | 08-07-2003 05:01 AM |