0

I'm trying to figure out how to access a parent method using a pointer to the child class. Any ideas? I looked for examples but found things that were more different than not. child access parent - different.

I have a test class with the following:

auto canvasObj = parseCanvas(one, two);
Three l_three = CanvasObject::ParentOfCanvas->getThree(); //this isn't working
//I also tried l_three = canvasObj->getThree(); (doesn't work)

where:

class CanvasObject final : public ParentOfCanvas
{....}

and:

class ParentOfCanvas : public AnotherObject
{
public:
     Three getThree();
       ...
}
Community
  • 1
  • 1
Michele
  • 3,617
  • 12
  • 47
  • 81

3 Answers3

3

It looks like you are attempting access the base class methods/data from a derived class instance.

Instead of :

auto canvasObj = parseCanvas(one, two); 
Three l_three = CanvasObject::ParentOfCanvas->getThree(); //this isn't working

Use :

auto canvasObj = parseCanvas(one, two);
Three l_three = canvasObj.getThree(); // or maybe canvasObj->getThree()

The reason yours didn't work is that it is, sort of, a mis-mash of attempting to access a 'static' member function instead of one on a particular instance. In my solution, I access the 'getThree' data on the canvas object that you created from the return of parseCanvas.

Alternately, if my solution isn't working, then perhaps parseCanvas is returning the wrong data type. That is, I'm expecting it to return CanvasObject, but you didn't provide the signature to that method. You may need to cast or replace auto with the exact type you are looking to have it be (CanvasObject*). Its posisble that parseCanvas might be returning a const type and getThree isn't marked as const, so you'd have trouble calling it.

Maybe if you provided the exact compiler error we could narrow it down further.

EDIT:

Based on your comments, the canvas object is returned a further base class pointer and you are attempting to call a derived class's method. This can't be done. You need to adjust your parseCanvas method to return a derived class pointer or you need to (VERY BAD IDEA) cast your pointer from base class to derived (if you really know that its really that type).

LawfulEvil
  • 2,267
  • 24
  • 46
1

You need to use the object variable something like:

Three l_three = canvasObj->getThree();
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
-1

I wound up doing this and it's working:

base_static_cast<AnotherObject, Three>(canvasObject)->getThree();

Correction. Upon further testing I had to do this:

static_cast(canvasobject.get())->getThree();

It was trying to get the info from the wrong place.

Michele
  • 3,617
  • 12
  • 47
  • 81
  • You really need to fix your design if your base classes need to invoke methods on derived classes. However, if you are stuck with the bad design, you should be using dynamic cast to do your downcasting, not static cast and always check the return value as failed downcasts will return a null pointer, so don't immediately dereference it. https://msdn.microsoft.com/en-us/library/cby9kycs.aspx – LawfulEvil Mar 11 '15 at 11:26