在C++中覆盖OpenGL的一个纹理包装器 我试图为C++中的一个纹理包装器分配赋值操作符,但是我有太多的问题,我不知道该去哪一个。
结构如下所示:在C++中覆盖OpenGL的一个纹理包装器 我试图为C++中的一个纹理包装器分配赋值操作符,但是我有太多的问题,我不知道该去哪一个。,c++,opengl,syntax,operator-overloading,software-design,C++,Opengl,Syntax,Operator Overloading,Software Design,结构如下所示: class Texture { protected: GLuint textureID; //!< OpenGL name of the texture GLuint target; //!< Target of the texture (e.g GL_TEXTURE_2D) int color_format; //!< The color format of the texture
class Texture
{
protected:
GLuint textureID; //!< OpenGL name of the texture
GLuint target; //!< Target of the texture (e.g GL_TEXTURE_2D)
int color_format; //!< The color format of the texture (e.g GL_RGBA)
int width; //!< width of the texture
int height; //!< height of the texture
std::string file;
public:
Texture(){}
Texture(std::string file_path, GLuint target);
~Texture();
};
因此,我的想法是在复制之前将右值中的textureID字段设置为0 nullptr
从本质上讲,这通过使前一个对象无效来防止浅拷贝。这是快速的,允许我做我需要的大部分事情,很容易实现。但这是非常误导的,因为
Texture t1, t2;
t1=t2;
也在默默地使t2成为无效对象
我应该怎么做?删除复制构造函数和复制赋值运算符。定义适当的移动操作 添加重置或重新绑定成员以用新纹理替换现有纹理
t.Reset(file1, target1);
避免创建一个临时对象,该对象被复制/移动,然后被销毁
t.Reset(file1, target1);