C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-20-2009, 02:08 PM   #16
Registered User
 
Join Date: Jul 2009
Posts: 14
Ok, i think I've got it now, i adjusted the "new Animal" to being a "new Dog" and added a call to the destructor and it works now.

Thanks again so much for being patient with me, I know I can be pretty hard to explain things to.
Aaronugio is offline   Reply With Quote
Old 07-20-2009, 05:19 PM   #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   Reply With Quote
Old 07-21-2009, 05:43 AM   #18
Registered User
 
hk_mp5kpdw's Avatar
 
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   Reply With Quote
Old 07-22-2009, 10:35 AM   #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   Reply With Quote
Old 07-22-2009, 10:41 AM   #20
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,353
Quote:
Originally Posted by Aaronugio
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?
It is not so much a matter of efficiency as a matter of correctness: you have a dog, but you want a cat, so if you can only have one animal, then you must get rid of the dog and then get the cat. You cannot turn the dog into a cat.
__________________
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   Reply With Quote
Old 07-22-2009, 10:58 AM   #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   Reply With Quote
Old 07-22-2009, 11:01 AM   #22
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,353
Quote:
Originally Posted by Aaronugio
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
Then you need to rethink your design.
__________________
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   Reply With Quote
Old 07-22-2009, 11:02 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-22-2009, 08:17 PM   #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   Reply With Quote
Old 07-22-2009, 08:54 PM   #25
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Reply

Tags
casting, classes, problem, vc++

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:41 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22