Is there a standard way beyond comments for identifying when a parent function is called in a child class?
Functions like BeginPlay() and HandleDestruction() are clear because they are labeled with things like “override” and “Super”. However, functions like Fire() and RotateTurret() are called from the child class with no indication that it is calling to a parent class. Is there a reason why we wouldn’t call something like Super::Fire() instead? (I assume that Super is only used when you are overriding a function?)
Super
is just a type alias for the parent class (aka super class)
void APawnTurret::HandleDestruction()
{
Super::HandleDestruction();
}
The parent class for APawnTurret
is APawnBase
so that code is equivalent to
void APawnTurret::HandleDestruction()
{
APawnBase::HandleDestruction();
}
It’s not a virtual function so It would mean the same thing just more verbose.
Essentially. Overriding a virtual function completely overrides the functionality. Calling Super::Foo
would mean you’re extending the parent function e.g.
class A
{
virtual void Foo() const { std::cout << "A::Foo()\n"; }
};
class B : public A
{
virtual void Foo() const override
{
//A::Foo();
std::cout << "B::Foo()\n";
}
};
int main()
{
B b;
b.Foo();
}
This would print B::Foo()
. If you uncomment the first line in B::Foo it would print
A::Foo()
B::Foo()
Thank you for such a detailed response! This really helped me get a better understanding of the workflow.
I am curious though, although it is more verbose, would it be considered better practice to call APawnBase::Fire() instead of just Fire() for readability sake? I know there’s probably not a standard but as someone who is hoping to work with larger teams in the future, I would like to make sure that I am learning the preferred methods now to make my code is as friendly to read as I can make it.
I’ve never actually seen that in the wild so I would say no.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.