Accessing func in daughter from base

This is a discussion on Accessing func in daughter from base within the C++ Programming forums, part of the General Programming Boards category; We have a mammal class, and a dog class which inherits mammal. Class dog has wagTail() func Since we can ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    Accessing func in daughter from base

    We have a mammal class, and a dog class which inherits mammal.
    Class dog has wagTail() func

    Since we can do...

    mammal *ptr = new dog;

    ... how can one access wagTail() using *ptr?

    The book I have mentions a way of explicitly calling it with the pointer but I can't find it. Any help would be appriciated.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You can't, and shouldn't be able to, directly access it in this fashion. Dogs can wag their tails, but mammals can't, because mammals may or may not have a tail.

    It makes no sense to be able to call the wag method from a mammal pointer; a dog can do anything a mammal can do because every dog is a mammal, but a mammal can't do everything a dog can, because every mammal is not a dog.

    You'd have to cast the base class (mammal) back to the derived class (dog), and then call the wag method with a dog pointer. But if you actually find yourself doing this in a real application, think long and hard if there isn't a better way to do things. Understanding when, and how, and how NOT to use inheritance and polymorphism is important.
    Last edited by Cat; 07-08-2003 at 07:53 PM.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    dynamic_cast - It will also return 0 if it failed to cast (i.e. if the mammal* was actually a cat* and not a dog*)

    For example:

    Code:
    mammal* ptr = new dog;
    dog* dogPtr = dynamic_cast<dog*>(ptr);
    if(dogPtr != 0)
       dogPtr->wagTail();
    The reason you can't directly is because it would violate some principles of inheritance. When you have a mammal*, you don't care about whether its a dog*, a bird* or a cat*. Each of those will have specialized functions (presumably), but you just want to access those that all mammals have (the functions in mammal).

    ** edit **
    I just realized that birds are not mammals.
    Last edited by Zach L.; 07-08-2003 at 07:56 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Zach L.
    When you have a mammal*, you don't care about whether its a dog*, a bird* or a cat*.
    You might care if it was a bird *, because a bird isn't a mammal at all. They're of class aves, not class mammalia

    Edit: Dammit, beaten to the punch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Base Converter Part 2
    By encyclopedia23 in forum C Programming
    Replies: 2
    Last Post: 12-30-2006, 02:42 PM
  3. Bit Computation Program
    By cisokay in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2005, 10:32 PM
  4. How to change number data from base 256 to base 16?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 12:19 AM
  5. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 04:14 PM

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