Pointing to derived class

This is a discussion on Pointing to derived class within the C++ Programming forums, part of the General Programming Boards category; Point is the base class and ThreeD is the derived class. I would like to create a new object dynamically ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Pointing to derived class

    Point is the base class and ThreeD is the derived class. I would like to create a new object dynamically from the derived class via the following code:

    Code:
    int main()
    {
    	Point *ptr = new ThreeD();
    	(*ptr).setXY(1,1); //base member function
    	(*ptr).setZ(1); //derived member function
    	(*ptr).display(); //polymorphic function
    
    	return 0;
    }
    The problem is that setZ(), a member function of the derived class, is unable to be called. Apparently the class created is that of the base class. What did I do wrong? Why isn't the object of the derived class?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,163
    The object is of type derived class. The problem is that ptr is a pointer to Point, so only member functions of Point can be called for that object.

    In effect, the initialisation of "Pointer ptr = new ThreeD()" tells the compiler that you only want to call Point's member functions on the ThreeD object. If ThreeD supports operations that Point doesn't, then those operations cannot be used on the object.

    From a design perspective, a point with three coordinates (X,Y,Z) is not a point in two-dimensional space unless Z is zero, so your derivation is invalid.

    Incidentally, (*ptr).setXY(1,1) is more usually written as ptr->setXY(1,1). Yes, they are equivalent: that is the way the -> operator is specified.
    Last edited by grumpy; 04-16-2011 at 01:16 AM.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I see, so instead I should have written "ThreeD ptr = new ThreeD()" instead?

    Got it to work, thanks!

    By the way, is there any difference writing ThreeD() and ThreeD for the object class?
    Last edited by 843; 04-16-2011 at 02:14 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,163
    That really depends what you're trying to achieve (which you haven't specified) but it's one option. (You left out an asterix though - this is C++, not Java).

    Don't forget, if you use operator new, it is generally a good idea to release the object (when you no longer need the object) with a "delete ptr;"
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Smart pointers are even better: SourceForge.net: Raw pointer issues - cpwiki
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. base class pointer pointing at derived class
    By mynickmynick in forum C++ Programming
    Replies: 11
    Last Post: 12-01-2008, 11:26 AM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  4. Replies: 2
    Last Post: 04-06-2005, 07:25 AM
  5. Replies: 1
    Last Post: 12-11-2002, 09:31 PM

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