简体   繁体   English

在向量C ++中存储类

[英]Storing classes in a vector C++

I am creating a game in C++/DirectX and I have came across a problem of storing sprites. 我正在用C ++ / DirectX创建一个游戏,我遇到了存储sprite的问题。 Currently I can create a sprite and then store it in a vector, doing this for one sprite works perfectly. 目前我可以创建一个精灵然后将它存储在一个向量中,这样做可以完美地实现一个精灵。 But, when I go to insert another sprite, the texture property of the previous sprite gets deleted. 但是,当我去插入另一个精灵时,前一个精灵的纹理属性会被删除。 I shall include some screen shots of breakpointing and some code. 我将包括一些断点和一些代码的屏幕截图。

The problem that I suspect is that the object is not being placed into the vector and is relying on the temporary object that is used to create the sprite. 我怀疑的问题是对象没有被放入向量中,而是依赖于用于创建精灵的临时对象。 Here are some screenshots: 以下是一些截图:

https://www.dropbox.com/s/g5xdlaqf35w6q57/1.png https://www.dropbox.com/s/g5xdlaqf35w6q57/1.png

https://www.dropbox.com/s/xmcyv611nqc27xc/2.png https://www.dropbox.com/s/xmcyv611nqc27xc/2.png

And some code: 还有一些代码:

// d2World.h
class d2World
{
public:
    // Some functions
    vector<d2Sprite> spritesList;
    // More stuff

private:
    d2Sprite *tempSprite;
    // Other private variables
};

// d2World.h
// Some other functions

// A new object is created by re-assigning it
tempSprite = new d2Sprite();

// When a the sprite is completed, add it to the vector
spritesList.push_back(*tempSprite);

// More stuff here

What I don't understand is why is it only the texture property being affected? 我不明白的是为什么只有纹理属性受到影响?

Thanks for your help. 谢谢你的帮助。

EDIT: Here is the header code for the d2Sprite class: 编辑:这是d2Sprite类的标题代码:

class d2Sprite
{
public:
    d2Sprite(void);
    ~d2Sprite(void);
    void Load(LPDIRECT3DTEXTURE9 tex);
    void Position(int x, int y);

    int x, y, frame, frameW, frameH, columns;
    float Rotation;

    D3DXVECTOR3 GetPosition();

    D3DXVECTOR2 Scale;
    D3DXVECTOR2 Center;
    D3DXVECTOR2 Translation;

    LPDIRECT3DTEXTURE9 texture;

    D3DCOLOR colour;
};

You are creating a copy of d2Sprite, and LPDIRECT3DTEXTURE9 seems to be a pointer... 你正在创建一个d2Sprite的副本,而LPDIRECT3DTEXTURE9似乎是一个指针......

spritesList.push_back(*tempSprite);

Why are you creating d2Sprite with new, and then copying them to the vector, you probably should have a 为什么要用new创建d2Sprite,然后将它们复制到向量中,你可能应该有一个

vector<d2Sprite*> spritesList;

and copy the pointer to the vector 并将指针复制到向量

spritesList.push_back(tempSprite);

Then call delete on the items in the vector when you don't need them anymore. 然后,当您不再需要它时,在向量中的项目上调用delete。

Without seeing the full definition of d2Sprite , I can only guess that the reason the texture property is being affected is that d2Sprite either doesn't have a correct copy constructor, or doesn't have a copy constructor at all, and the default copy constructor provided by the compiler doesn't do the right thing to copy the texture property correctly. 在没有看到d2Sprite的完整定义的d2Sprite ,我只能猜测纹理属性受影响的原因是d2Sprite要么没有正确的复制构造函数,要么根本没有复制构造函数,以及默认的复制构造函数由编译器提供的做法不正确地正确复制纹理属性。

When you insert an object into an STL container, it actually inserts a copy of the object, not the object itself. 将对象插入STL容器时,它实际上插入了对象的副本 ,而不是对象本身。 If you don't want copies, consider making the container hold pointers to the objects (so that only the pointer itself is copied, not the object). 如果您不想要副本,请考虑使容器保持指向对象的指针(这样只复制指针本身,而不是对象)。

This is only a guess, since there isn't enough code shown to know for sure. 这只是猜测,因为没有足够的代码可以肯定地知道。 Here is what I think is happening: 以下是我认为正在发生的事情:

d2Sprite has no copy constructor, and the vector contains d2Sprite objects, not pointers. d2Sprite没有复制构造函数,向量包含d2Sprite对象,而不是指针。 When the temporary object tempSprite is added to the vector, it is copied, so the raw pointer LPDIRECT3DTEXTURE9 is copied bit-for-bit. 当临时对象tempSprite被添加到向量时,它被复制,因此原始指针LPDIRECT3DTEXTURE9被逐位复制。

I am going to guess here that you are deleting or overwriting the temporary object tempSprite. 我在这里猜测你正在删除或覆盖临时对象tempSprite。 When the temporary is deleted or overwritten, the d2Sprite in the vector now has a LPDIRECT3DTEXTURE9 that points to freed memory. 当临时被删除或覆盖时,向量中的d2Sprite现在有一个指向释放内存的LPDIRECT3DTEXTURE9。 This will cause the texture to be lost. 这将导致纹理丢失。 Note that it may keep displaying correctly until the memory gets overwritten, usually when the next memory allocation is done. 请注意,它可能会一直正常显示,直到内存被覆盖,通常是在下一次内存分配完成时。 When the next d2Sprite is created, memory will be allocated, and your (already freed) DIRECT3DTEXTURE9 object will likely be overwritten at that time. 创建下一个d2Sprite时,将分配内存,此时可能会覆盖您已(已释放)的DIRECT3DTEXTURE9对象。

Generally speaking, issues like this can be avoided if you use RAII, specifically smart pointers like boost::shared_ptr or (in C++ 11) std::shared_ptr. 一般来说,如果你使用RAII,特别是像boost :: shared_ptr或(在C ++ 11中)std :: shared_ptr这样的智能指针,可以避免这样的问题。 Used properly, RAII takes care of these kinds of allocated memory lifetime problems. 如果使用得当,RAII会处理这些分配的内存生存期问题。 Without RAII, you must track the lifetime of all allocated resources. 没有RAII, 必须跟踪所有分配的资源的生命周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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