Questions related to virtual and Super

Please help me to clarify the usage of virtual and Super. My understaning is as below(Please correct me since I could be terribly wrong):

1.Virtual is used when a function needs to be inherited by its child classes. With virtual, this function can be inherited and overridden to cater to neeed of child classes. Therefor, it’s normally used under public and not protected or private.

2.When calling function, in order to guarentee child classes get all the functionality, Super needs to be used. But what’s the consequence of not using Super? When I first wrote the input biding code, I forgot to include Super and nothing happened.

This is incorrect. Members are always inherited, using public for the inheritance means that they will remain the same access level e.g.

class Base
{
public:
    void PublicFoo();
protected:
    void ProtectedFoo();
private:
    void PrivateFoo();
};

class Derived : public Base
{
// All of these are inherited
// public:
//     void PublicFoo();
// protected:
//     void ProtectedFoo();
// private:
//     void PrivateFoo();
};

Changing Derived to be

class Derived : protected Base

Would inherit PublicFoo() as a protected member instead and having

class Derived : private Base
// or simply, as private is default for classes
class Derived : Base

Would inherit everything as private members.


The point of virtual functions is for “dynamic dispatch”. Given a base pointer (or reference) you can call a virtual function and it will dispatch to the correct function.

#include <cstdio>

struct Animal
{
    virtual void Speak() const 
    {
        puts("Animal");
    }
    virtual ~Animal() = default;
};

struct Dog : Animal
{
    void Speak() const override
    {
        puts("Woof!");
    }
};

struct Cat :  Animal
{
    void Speak() const override
    {
        puts("Meow!");
    }
};

// this will call the correct function.
void Talk(const Animal& A)
{
    A.Speak();
}

int main()
{
    Cat C;
    Dog D;

    C.Speak();
    D.Speak();

    puts("");

    Talk(C);
    Talk(D);
}

And if you remove the virtual-ness Talk no longer calls the correct function for the actual type.
Demo:
Compiler Explorer
Compiler Explorer

The base function doesn’t get called, if the parent class’ BeginPlay function needs to do something then not calling it would mean it doesn’t do the stuff it needs to.

Thanks for answering. Wow that’s a lot to take in. I’ll read them carefully. While I am at it, one more question, why is setting up player input being called every frame? I don’t see it under the tick function.

That is the role of the input component. You’ve told it (via the BindAxis functions) to call a particular member function on the instance of the Tank (via this) for certain input axis mappings

Input Action And Axis Mappings In UE4 - Unreal Engine

Thanks a lot. That artical is really helpful.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms