简体   繁体   中英

How to copy a pointer to incomplete type - C++

I'm trying to copy the contents of the pointer myTexture into the pointer texture (a class member), as shown in the code below:

Sprite::Sprite(SDL_Renderer *renderer, SDL_Texture *myTexture) {

    // Initialize texture
    this->texture = myTexture;
}

However, I need to have texture not just point to the same object as myTexture , because I want to be able to modify texture without affecting myTexture as well. I have tried using the copy constructor, but the compiler says that incomplete types are not allowed. I'm not really sure as to how I can create a copy of this object without pointing to the same object.

Thanks in advance for any help.

You cannot call the copy constructor, or generally access any member of an incomplete type. After all, that's what makes it incomplete; you are not supposed to know that the type even supports copying, or anything else.

I'd say just include the whole type rather than just forward-declaring it, and be done with it.


Edit: I might add that we are talking about an SDL texture type here. This type doesn't look like it even supports copying. It has, for example, "prev" and "next" pointers, and a pointer which seems to point back to a renderer object. Hardly a value class.

It looks like you should reconsider your design as a whole here, and the real question is not about incomplete types but "How to copy SDL_Texture contents."

I think I found a solution. It's not exactly copying the pointer, but what I did is, because I'm using SDL2, I changed the render target to this->texture , and then used SDL_RenderCopy() to put the original texture on the new one. This was the easiest solution I found, unfortunately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM