Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

c++ - Register in Constructor (over inheritance), after copy-constructor no longer accessible

I'm supposed to program a game in C++ and OpenGL. I tried to structure it after Unity, so I have Gameobjects that inherit the Components they need. My problem is my renderer. This component contains a shape, Vao, Vbo and a Color. In this constructor I am trying to register the object in my GameView

LineRenderer::LineRenderer(){
    shape = { 1.0,1.0,0.0,
        1.0,0.0,0.0,
        0.0,0.0,0.0,
        0.0,1.0,0.0 };
    InitializeVertexArrayObject(&VAO, &VBO, shape);
    GameView::add(this);
}

The GameView has a static vector as member:

static std::vector<LineRenderer*> items;

And the add function just adds the pointer to the vector:

void GameView::add(LineRenderer* item){
    items.push_back(item);
}

Now, obviously, this is not working because every time I want to render the objects in the GameView they no longer exists. Google says this has something to do with the Copy Constructor and it seems the way I want to do this is not possible.

The Question is, is it even possible to inherit from a class that registers itself somewhere else (if the Object is always copied into the variable it's assigned to)? And, if not, what would be the best way to implement behaviour similar to this?

question from:https://stackoverflow.com/questions/65887106/register-in-constructor-over-inheritance-after-copy-constructor-no-longer-acc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unless your copying the object itself, copying pointers wont affect the constructor after you create it. The vector will just copy/ move it in. Lets say that your creating the LineRenderer like below,

void SomeFunction()
{
    ...
    LineRenderer* pRenderer = new LineRenderer();    // The constructor will be called here.
    ...
    delete pRenderer;    // The destructor will be called here.
}

Now, obviously this is not working because every time I want to render the objects in the GameView they no longer exists.

Yes, if your allocating them in the stack. Use heap allocation as I said before and things will work out.

Note: If you allocate something in the heap, make sure to delete it!

is it even possible to inherit from a Class that registers itself somewhere else (If the Object is always copied into the variable its assigned to)?

In your case, yes, you can. And your working with pointers here, so your not copying the object itself, your just copying the address of it.

And if not what would be the best way to implement behavior similar to this?

As you stated before, if you want your game to work like unity, your approach is somewhat different. Unity uses Entity Component System while you use the same thing which unity tries to work around. But looking at your implementation, what you need is to avoid static allocating those objects, rather than that allocate them in the heap. Remember, you'll have to manage its life time then!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...