Pointers to derived classes.

This is a discussion on Pointers to derived classes. within the C++ Programming forums, part of the General Programming Boards category; I declare class 1. Class 2 is derived (publicly) from class 1. If I have a pointer to an instance ...

  1. #1
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    Pointers to derived classes.

    I declare class 1. Class 2 is derived (publicly) from class 1. If I have a pointer to an instance of class 1, can I initialize it to the address of an instance of type class 2?

    How is this dealt with when dealing with mulitple inheritance? Friend functions?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I declare class 1. Class 2 is derived (publicly) from class 1. If I have a pointer to an instance of class 1, can I initialize it to the address of an instance of type class 2?
    Yes, but unless you're using virtual methods you'll be using the class 2 object as a class 1 object (unless you cast).

    [/quote]How is this dealt with when dealing with mulitple inheritance? Friend functions?[/quote]

    You'll have to be more specific.
    zen

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    More specificness... if that's a word.

    By that I meant, could I use the pointer if class 2 was derived from class 1, AND class 3 (multiple inheritance), and what if class 2 was declared as a friend of class 1 (the friend function thing).

    Thanks for the help.

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Yes, but it would still only be able to use class 1's interface as the class 1 pointer doesn't know anything about class 2 or 3. I'm not sure that declaring class 2 as a friend of class 1 would have any effect, but I'm don't know what you're trying to do.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange gcc warning messages with derived classes
    By skewray in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2007, 04:46 AM
  2. vector of derived classes
    By p3p in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2005, 07:28 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Pointers to classes
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2002, 09:25 PM
  5. List of derived classes
    By Asmodan in forum C++ Programming
    Replies: 1
    Last Post: 05-28-2002, 08:46 PM

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