Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delegate a constructor correctly

Tags:

c++

When I use a second constructor, the object is still NULL after the call. I'm pretty sure that it doesn't work this way but how else should I do this? I have no idea how to make the Transform struct in the correct delegation (I know how to delegate a constructor because I did it for the Vector3D):

object::object(char* filename, std::string name, Transform transform) : m_pFilename(filename), m_name(name), m_transform(transform) {}

object::object(char* filename, std::string name)
{
    Transform transform = {
        Vector3D(0.0f, 0.0f, 0.0f),
        Vector3D(0.0f, 0.0f, 0.0f),
        Vector3D(1.0f, 1.0f, 1.0f),
    };
    object(filename, name, transform);
}
like image 285
Rafiwui Avatar asked Apr 09 '26 09:04

Rafiwui


1 Answers

That's not what a delegating constructor is.

A delegating constructor gets invoked using the same syntax that's used for invoking superclasses' constructors (more, or less).

So, a delegating constructor here would be:

object::object(char* filename, std::string name)
           : object(filename, name, /* here be dragons */)
{
}

With the "/* here be dragons*/" part consiting of the necessary voodoo that constructs the instance of your Transform object. The "here be dragons" part could be (depending on what your Transform is):

Transform{
    Vector3D(0.0f, 0.0f, 0.0f),
    Vector3D(0.0f, 0.0f, 0.0f),
    Vector3D(1.0f, 1.0f, 1.0f),
}

That'll probably work. Or, it could be:

Transform(
    Vector3D(0.0f, 0.0f, 0.0f),
    Vector3D(0.0f, 0.0f, 0.0f),
    Vector3D(1.0f, 1.0f, 1.0f),
)

Or, in any case, it can always be:

make_transform()

With this make_transform() defined in some convenient place:

Transform make_transform()
{
    Transform transform = {
        Vector3D(0.0f, 0.0f, 0.0f),
        Vector3D(0.0f, 0.0f, 0.0f),
        Vector3D(1.0f, 1.0f, 1.0f),
    };

    return transform;
}

Can't authoritatively state which one; depends on what the Transform is, but one or the other should work. But the point is that a delegating constructor is not supposedly calling one constructor from the body of another constructor. A delegated-to constructor is a constructor call that gets invoked before the delegating constructor's body, as if it were a superclass (more or less, as I mentioned).

The thing about delegating constructors is that you can't really do a lot before the constructor gets delegated. The only allowed syntax for delegating constructors is the one I showed. This means that you can't execute complex code, as part of the constructor's body, in order to come up with the arguments to the delegating constructor's call. The delegating call must be in the initializer section. This is somewhat limiting, but it is what it is. Arcane trickery is quite common, including using gcc's ({ ... }) extension.

like image 146
Sam Varshavchik Avatar answered Apr 10 '26 23:04

Sam Varshavchik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!