C++ 析构函数(附代码示例)

标签 c++ c++11

我刚刚从 Java 切换到 C++,到目前为止一切都进展顺利。语言有点难,但我觉得我好像在 catch 。不过,我有一个关于析构函数的问题,为此我将提供我当前的代码,希望有人可以澄清我应该继续做什么以及如何进行。

我正在用 OpenGL 编写游戏并创建了三个类:Vector3D、Dimension3D 和 Cuboid。我的问题是这样开始的,我的长方体有一个 Vector3D 和 Dimension3D 的实例,您很快就会看到。我需要知道我的 Cuboid 类一旦被标记为删除后会发生什么(确切的例程)。或者更准确地说,如果我需要在此类事件发生时显式销毁 Vector3D 和 Dimension3D 实例。我希望我足够充分地阐明了这个问题。

这是我的类(class)。

(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;
}

(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_

(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;
}

(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_

最后,我正在进行的工作 Cuboid.cpp 和 Cuboid.h

(立方体.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(){

}

(立方体.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_

我在析构函数中的 Cuboid.cpp 中留下了评论。我想知道我是否应该删除那里的 Vector3D 和 Dimension3D,以及一个显示执行此操作的最佳方法的示例。 IE:表达此功能的任何通用约定。

如果我的问题不够充分,我将非常乐意提供进一步的说明。另外,我确信还有其他类似的问题,但是,我需要在我自己的代码中看到它才能完全掌握它。 (奇怪的学习风格),所以如果这变成了重复,请原谅我。

最佳答案

在这种特殊情况下,您不需要任何明确的销毁代码。

这是因为您使用的是直接成员:

class Cuboid {
public:
    Vector3D location;
};

这意味着Vector3D对象被嵌入Cuboid的内存中,并与Cuboid一起自动分配和释放

如果您有一个指向对象作为成员的指针(例如 Vector3D *location)而不是成员本身,情况就会不同。在这种情况下,您还必须为 location 显式分配内存,并在析构函数中显式释放它。

关于C++ 析构函数(附代码示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22095902/

相关文章:

c++ - 类成员值被前一个成员覆盖

c++ - ‘is_trivially_copyable’ 不是 ‘std’ 的成员

c++ - 在 C++ 中获取拉丁字符

c++ - 分配/计算学生成绩的循环 (c++)

c++ - 如何处理某些元素中带有 nul char 的 CSV 行?

c++ - 父对象构造期间不受限制的 union 成员生命周期

c++ - 使用静态常量结构对相关的类常量进行分组 (C++11)

c++ - 为什么我的成员函数中的 lambda 表达式的主体没有被执行?

c++ - 从 C++ 开始,密码验证器

c++ - 此代码是否会导致内存泄漏(Arduino)