1

I created a GameObject class in which I have 2 constructors. One default GameObject () = default; but in which at me in sheet of initializations variables are initialized. And in the second constructor GameObject(vec3 pos, vec3 rot, vec3 scale), and I want to add a default constructor in the init list so that it has the same parameters.

But I need to modify some variables a bit, for example transform(Transformation((*this)) to transform(Transformation((*this), position, rotation, scale).

But I can't do it because I'm have a error

"a delegated constructor cannot have other mem-initialization".

This a template code:

class GameObject
{
    //params .....


    GameObject::GameObject()
        :  transform(Transformation((*this))), render(true)

    {
    }

    GameObject::GameObject(glm::vec3 position, glm::vec3 rotation, glm::vec3 scale)
        : GameObject(),
          transform(Transformation((*this), position, rotation, scale) //here a error
    {
    }
}

How can I fix that? Or maybe C++ have another way to do that?

16
  • Please provide minimal reproducible example Commented Dec 14, 2019 at 15:47
  • 1
    This is simple situation, why i need to drop here all my code? Create a class with 2 constructors and add first constructor to initialize list in second constructor, with slightly changes some params with mem initialize list. I already showed this example. Commented Dec 14, 2019 at 15:52
  • No one is asking you to drop all your code. Just the smallest possible working piece of code which replicates your problem. Commented Dec 14, 2019 at 15:53
  • 1
    @CinCout I don't really see the point. The point of failure is obvious from the code provided. You don't need to #include <glm> or add a main method to recognize the problem; you just need to know how delegating constructors work. Commented Dec 14, 2019 at 15:54
  • 1
    @KennyTutorials His point was that it's easier to help you if you provide a short compilable program that reproduces the issue, and the exact error message. This time the issue was more or less trivial, so you were able to get your help regardless, but that might not happen every time. Commented Dec 14, 2019 at 16:26

1 Answer 1

3

Once you are in the body of a constructor, all subobjects (base classes and non-static data members) of that type are considered live, valid objects and therefore have to have been previously initialized. This is the purpose of a constructor's member initialization list.

However, a member initialization list is allowed to do something odd. It can essentially delegate the primary initialization of the object and its subobjects to some other constructor. This is what happens when you invoke the class's constructor in a member initialization list. A constructor which does this is called a delegating constructor.

This of course means that, by the time control returns to the delegating constructor, all of the members have already been initialized. Therefore, a member initialization list which delegates to another constructor delegates all member initialization to that constructor. It cannot have additional member initializers, since those members have already been initialized.

That's why you're getting the error.

What you really want to do is have the default constructor and your other constructor both delegate to a constructor that takes a Transform object, which will be used to initialize the appropriate member:

    GameObject::GameObject()
        :  GameObject(Transformation((*this))), render(true))
    {
    }

    GameObject::GameObject(glm::vec3 position, glm::vec3 rotation, glm::vec3 scale)
        : GameObject(Transformation((*this), position, rotation, scale))
    {
    }

private:

    explicit GameObject::GameObject(Transformation &&tf) : transform(std::move(tf))
    {
    }
Sign up to request clarification or add additional context in comments.

6 Comments

I cant understand one thing, when we set to list GameObject(Transformation((*this), position, rotation, scale) its also calls default constructor or what?
@KennyTutorials: No; it's not a delegating constructor, so it proceeds just like any normal constructor. The default constructor delegates to it, not the other way around. The stuff you would have put into the default constructor instead goes into that constructor.
If you have time can on much detail to tell, and the I not very understand. I will be very grateful)
@KennyTutorials: I don't really know what more I can say. I've described how delegating constructors work and what their limitations are. There just aren't any more details than those I've stated. Outside of dealing with the deep magic of the C++ object model, object lifetimes, and so forth but that's a huge thing that's essentially irrelevant to your problem.
When i call this constructor GameObject::GameObject(glm::vec3 position, glm::vec3 rotation, glm::vec3 scale) {} i have new error " error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Transformation'"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.