Hi there, C++ noob here.
I’m trying to get the following approach working (simplified code):
Parent Class:
class Parent
{
// Constructor
Parent::Parent(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
// Do some stuff
SetSomeVariables();
}
// Set default values for parent
void Parent::SetSomeVariables()
{
MyVar = 20;
}
}
Child Class:
class Child
{
// Constructor
Child::Child(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
// Do some other stuff - we know parent constructor will be called before us
}
// Set default values for child
void Child::SetSomeVariables()
{
MyVar = 30;
}
}
Now what I’d like to do is that when the Parent class constructor is called, it must use the child version of “SetSomeVariables” as I’m overriding it (“SetSomeVariables” is declared as virtual in the parent, and set to OVERRIDE on the child) This does not happen however. How could I get this working, or am I totally missing C++ inheritance?
I think constructor as a funtion is overriden too, but inhirence works when you use varables set direcly, it might be some trick in engine source code or even UHT. You might try to do something with Super
Well, reading up now on C++ constructors and inheritance, it seems that the parent constructor is called first, and then the child constructor.
So what happens is that in my case, the whole parent class will be constructed, spawning the assets using the parent reference values, and then only afterwards, the child constructor is called. I think this pretty much means that I will have to copy paste the whole parent class constructor into the child class, and at the start of the child constructor, set the specific reference values for the child. This becomes a bit cumbersome if I want to use ConstructorHelpers, as they can only be used inside a constructor.
So in short, it seems like there’s no way to avoid fully running the constructor code of both the parent and the child, and there’s no way to re-use the asset spawning logic created in the parent class.
Why would you want to run high level init code in class constructor? This is actually possible in c++, as you might have already found out on Stackoverflow, but that approach is an utter hack.
You have a choice of init functions, like OnConstruction and PostSpawn*, they are called automatically by the engine on every actors’ construction. For UObjects, similar functions also exist. So overall, do you have a specific reason for doing your initialization in class constructor instead? I can’t think of a real life scenario where construction functions do not work.
Yea, but default variables states and components should be set in constructor… at least looking engine source code it seems it a engine convention and he seems it want to do that with function call instead of directly in constructor. And as he mentioned constructor helpers works exclusively in constructor or else your code wont build.
Thanks very much for all the input guys, I’ve updated my related post on the forums: link text