# Calling an overridden function on the child class

**URL:** <https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 27, 2016, 2:13pm UTC](https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582 "2016-06-27T14:13:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mago314](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mago314](https://forums.unrealengine.com/u/mago314)\
**Post date:** [June 27, 2016, 2:13pm UTC](https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582/1 "2016-06-27T14:13:18Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Philippe\_St-Amand](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Philippe\_St-Amand](https://forums.unrealengine.com/u/Philippe_St-Amand)\
**Post date:** [June 27, 2016, 11:00pm UTC](https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582/2 "2016-06-27T23:00:25Z")

</div>

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

MyClassA:

```auto

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

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

```

MyClassB:

```auto

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…

```auto

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

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [June 28, 2016, 8:46am UTC](https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582/3 "2016-06-28T08:46:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mago314](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mago314](https://forums.unrealengine.com/u/mago314)\
**Post date:** [June 28, 2016, 10:18am UTC](https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582/4 "2016-06-28T10:18:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [June 28, 2016, 10:32am UTC](https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582/5 "2016-06-28T10:32:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mago314](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mago314](https://forums.unrealengine.com/u/mago314)\
**Post date:** [June 28, 2016, 12:16pm UTC](https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582/6 "2016-06-28T12:16:24Z")

</div>

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 😛

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

---

<div class="post-metadata">

**Author:** ![Teh\_Masterer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Teh\_Masterer](https://forums.unrealengine.com/u/Teh_Masterer)\
**Post date:** [June 28, 2016, 3:55pm UTC](https://forums.unrealengine.com/t/calling-an-overridden-function-on-the-child-class/67582/7 "2016-06-28T15:55:24Z")

</div>

> [@mago314;555782](#):
>
> 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.

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.

```auto

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.
