Download

Calling an overridden function on the child class

Hello there. I would like some opinions and advice on a problem I’m tackling:

There’s a ParentClass defined in C++, which has some virtual functions such as Func1. Then I have ChildClassA and ChildClassB implemented also in C++, both of which override the Func1 in their own way.

MyPawn then has a variable called MySelectedElement of type ParentClass, and when certain button is pressed, it should call Func1 on MySelectedElement.

However, and here is where I hit a brick wall: As the variable is of type ParentClass, the function that gets called is not the overriden one but the base function.

I think this can be solved by trying to cast MySelectedElement to each of the Child classes, but that looks clumsy and inefficient.

Is there a better way to do it?

Thanks in advance :slight_smile:

I had some kind of brain fart and couldn’t remember what was what, so I made a quick test:

MyClassA:



class GAMETESTPROJECT_API MyClassA
{
public:
    MyClassA();
    ~MyClassA();

    virtual void TestMethod()
    {
        GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Cyan, TEXT("I AM CLASS A"));
    }
};


MyClassB:



class GAMETESTPROJECT_API MyClassB : public MyClassA
{
public:
    MyClassB();
    ~MyClassB();
    
    virtual void TestMethod() override
    {
        GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Cyan, TEXT("I AM CLASS B"));
    }
};


And then in some Actor…



void ATestMaster::BeginPlay()
{
    Super::BeginPlay();

    MyClassA* test = new MyClassB();
    test ->TestMethod();

    delete test;
    test = nullptr;
}


This prints…
“I AM CLASS B”

So it’s supposed to do what you initially wanted it to do. Maybe you’re missing something… Pointers? Override keyword?
Try making some basic tests

Override keyword doesn’t actually change anything, but not using it is the most common cause of this problem. It’s basically just a safety check to confirm you’ve spelled the function name and declared the parameters/qualifiers exactly the same in both the child and parent. So make sure you use the override keyword to confirm that you haven’t made such a mistake.

Failing that, the only other explanation is that your object is actually of type parent and not child.

[MENTION=71009]Philippe St-Amand[/MENTION]: You need to wrap in [code][/code] tags (without the underscores). Click the BBCode link at the bottom for info.

My problem is exactly that: The variable is of type parent, not child. However, if I make of type childA, then I wont be able to store the ChildB, and viceversa

My question is on how can this be solved.

I said that perhaps the object is of type parent. It doesn’t matter what type the variable is, if the *object * is a child, then the child implementation of the function will be called. That’s the whole point of virtual functions.

Thanks kamrann! I made some tests on a clean project and you’re right. I guess the problem is somewhere else. I’ll keep on searching :stuck_out_tongue:

Edit: It’s solved! I was using the wrong parent on a blueprint. Thank you all :smiley:

What you want is to use polymorphism. It’s not clear whether or not your variable MySelectedElement is a pointer or not. Your variable needs to be a pointer to the base class in order to use polymorphism.


ParentClass * MySelectedElement;

MySelectedElement = new childA;
MySelectedElement->Func1(); //this will call childA::Func1

delete MySelectedElement;

MySelectedElement = new childB;
MySelectedElement->Func1(); //this will call childB::Func1


So what you want to do is store a pointer to the base class. Then you can allocate any child class and assign it to that base class pointer. You can call any functions defined in the base class, and if those functions are overridden in the child class, the child class functions will be called instead of the base class functions.