Thread: Casting issues...

  1. #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.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #18
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your destructor's should be virtual.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #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?

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #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

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #25
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  2. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM

Tags for this Thread