我刚从Java切换到C ++,到目前为止一切进展顺利。语言有点难,但是我感觉好像正在流行。不过,我有一个关于析构函数的问题,为此,我将提供我当前的代码,希望有人可以对我应该做什么和如何进行澄清。
我正在用OpenGL编写游戏,并创建了三个类:Vector3D,Dimension3D和Cuboid。我的问题是这样开始的,我的长方体有一个Vector3D和Dimension3D的实例,您将很快看到它们。我需要知道将Cuboid类标记为删除后如何处理(确切的例程)。或更准确地说,如果我需要在发生此类事件时显式销毁Vector3D和Dimension3D实例。我希望我足够清楚地阐明这个问题。
这是我的课。
(Vector3D.cpp)
#include "Vector3D.h"
Vector3D::Vector3D(){
}
Vector3D::Vector3D( const float& x , const float& y , const float& z ){
this->x = x;
this->y = y;
this->z = z;
}
Vector3D::~Vector3D(){
}
void Vector3D::setX( const float& x ){
this->x = x;
}
void Vector3D::setY( const float& y ){
this->y = y;
}
void Vector3D::setZ( const float& z ){
this->z = z;
}
float Vector3D::getX(){
return x;
}
float Vector3D::getY(){
return y;
}
float Vector3D::getZ(){
return z;
}
Run Code Online (Sandbox Code Playgroud)
(Vector3D.h)
#ifndef NE3_Vector3D_H_
#define NE3_Vector3D_H_
class Vector3D{
public:
Vector3D();
Vector3D( const float& , const float& , const float& );
~Vector3D();
void setX( const float& );
void setY( const float& );
void setZ( const float& );
void setPosition( const float& , const float& , const float& );
float getX();
float getY();
float getZ();
float x;
float y;
float z;
private:
// Private Members Go Here
};
#endif // NE3_Vector3D_H_
Run Code Online (Sandbox Code Playgroud)
(Dimension3D.cpp)
#include "Dimension3D.h"
Dimension3D::Dimension3D(){
}
Dimension3D::Dimension3D( const float& width , const float& height , const float& depth ){
this->width = width;
this->height = height;
this->depth = depth;
}
Dimension3D::~Dimension3D(){
}
void Dimension3D::setWidth( const float& width ){
this->width = width;
}
void Dimension3D::setHeight( const float& height ){
this->height = height;
}
void Dimension3D::setDepth( const float& depth ){
this->depth = depth;
}
float Dimension3D::getWidth(){
return width;
}
float Dimension3D::getHeight(){
return height;
}
float Dimension3D::getDepth(){
return depth;
}
Run Code Online (Sandbox Code Playgroud)
(Dimension3D.h)
#ifndef NE3_Dimension3D_H_
#define NE3_Dimension3D_H_
class Dimension3D{
public:
Dimension3D();
Dimension3D( const float& , const float& , const float& );
~Dimension3D();
void setWidth( const float& );
void setHeight( const float& );
void setDepth( const float& );
void setSize( const float& , const float& , const float& );
float getWidth();
float getHeight();
float getDepth();
float width;
float height;
float depth;
private:
// Private Members Go Here
};
#endif // NE3_Dimension3D_H_
Run Code Online (Sandbox Code Playgroud)
最后,我的工作正在进行中Cuboid.cpp和Cuboid.h
(Cuboid.cpp)
#include "Cuboid.h"
Cuboid::Cuboid(){
}
Cuboid::Cuboid(const Vector3D& location, const Dimension3D& dimension){
this->location = location;
this->dimension = dimension;
}
Cuboid::~Cuboid(){
// Do i do delete both location and dimension here?
}
void Cuboid::drawImmediate(){
}
void Cuboid::drawVBO(){
}
Run Code Online (Sandbox Code Playgroud)
(Cuboid.h)
#ifndef NE3_CUBOID_H_
#define NE3_CUBOID_H_
#include "Vector3D.h"
#include "Dimension3D.h"
class Cuboid{
public:
Cuboid();
Cuboid( const Vector3D& , const Dimension3D& );
~Cuboid();
void drawImmediate();
void drawVBO();
Vector3D location;
Dimension3D dimension;
private:
};
#endif // NE3_CUBOID_H_
Run Code Online (Sandbox Code Playgroud)
我在析构函数内的Cuboid.cpp中发表了评论。我想知道是否应该在此处删除Vector3D和Dimension3D,并举一个示例,说明执行此操作的最佳方法。IE:表达此功能的所有常用约定。
如果我的问题不够充分,我将很乐意提供进一步的说明。另外,我敢肯定还有其他类似的问题,但是,我需要在自己的代码中看到它才能完全掌握它。(奇怪的学习风格),所以如果这变成重复的话,请原谅我。
在这种情况下,您不需要任何明确的销毁代码。
原因是您使用的是直接成员:
class Cuboid {
public:
Vector3D location;
};
Run Code Online (Sandbox Code Playgroud)
这意味着该Vector3D对象将嵌入到的内存中,Cuboid并与一起自动分配和释放Cuboid。
如果您有一个指向对象的指针作为成员(例如Vector3D *location),而不是成员本身,则情况会不同。在这种情况下,您还必须为显式分配内存location,并在析构函数中显式释放它。