Asssign a derived class pointer to a base class?

This is a discussion on Asssign a derived class pointer to a base class? within the C++ Programming forums, part of the General Programming Boards category; Hello, I have two classes. One entitled "Layer" and the other entitled "Point", which is derived from "Layer". Would it ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Asssign a derived class pointer to a base class?

    Hello,

    I have two classes. One entitled "Layer" and the other entitled "Point", which is derived from "Layer".

    Would it be possible to do the following:

    Layer * LayerPtr = new Point();

    . . . and use the functions offered by the Point object through the Layer pointer? For instance, I would like to do the following granted the LayerPtr points to a Point object:

    LayerPtr->PublicPointFunction();

    Is this possible? I am new to inheritance and polymorphism. What declarations would be necessary in the class definitions for Layer and/or Point?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    If you want to access functions in Point by not in Layer with a Layer*, you need to cast the pointer to a Layer.

    dynamic_cast<Point*>(layerPtr)->PublicPointFunction();

    It's ugly, and it should be. Consider the possibility of adding a pure virtual to Layer if no specific implementation makes sense, but every Layer subclass should decide the behavior for themself.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Thanks, but your syntax gives me errors upon the dynamic cast.

    Here is what I am trying to do.

    Code:
    	//create a layer object
    	Layer * LayerPtr = new Point("aluspts_0.shp", "C:\pec\complete\vector");
    	Point * PointPtr = dynamic_cast<Point *>(LayerPtr);
    
    	PointPtr->setColors("27", "27", "27");
    setColors() is a public function of the Point class.

    The line with the dynamic cast is giving me errors.

    What am I doing wrong?
    Last edited by VanJay011379; 07-25-2002 at 09:32 AM.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What is the compiler error message?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM

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