我应该删除 C++ 类的字符串成员吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2365624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:13:31  来源:igfitidea点击:

Should I delete the string members of a C++ class?

c++string

提问by Blade3

If I have the following declaration:

如果我有以下声明:

#include <iostream>
#include <string>

class DEMData
{
private:
    int bitFldPos;
    int bytFldPos;
    std::string byteOrder;
    std::string desS;
    std::string engUnit;
    std::string oTag;
    std::string valType;
    int idx;
public:
    DEMData();
    DEMData(const DEMData &d);
    void SetIndex(int idx);
    int GetIndex() const;
    void SetValType(const char* valType);
    const char* GetValType() const;
    void SetOTag(const char* oTag);
    const char* GetOTag() const;
    void SetEngUnit(const char* engUnit);
    const char* GetEngUnit() const;
    void SetDesS(const char* desS);
    const char* GetDesS() const;
    void SetByteOrder(const char* byteOrder);
    const char* GetByteOrder() const;
    void SetBytFldPos(int bytFldPos);
    int GetBytFldPos() const;
    void SetBitFldPos(int bitFldPos);
    int GetBitFldPos() const;
    friend std::ostream &operator<<(std::ostream &stream, DEMData d);
    bool operator==(const DEMData &d) const;
    ~DEMData();
};

what code should be in the destructor? Should I "delete" the std::stringfields?

析构函数中应该包含哪些代码?我应该“删除”这些std::string字段吗?

回答by Liz Albin

Your destructor only has to destroy the members for which you allocate resources. so no, you don't "delete" strings.

您的析构函数只需销毁您为其分配资源的成员。所以不,你不会“删除”字符串。

You deletepointers you allocate with new

删除你用new分配的指针

Your destructor doesn't have to be more than

你的析构函数不必超过

~DEMData()
{
}

回答by Gregor Brandt

No, the std::string members are allocated automatically on the stack or heap for you and are cleaned up automatically. You do not have to do anything special in your destructor to handle the strings.

不,std::string 成员会在堆栈或堆上为您自动分配并自动清理。您不必在析构函数中执行任何特殊操作来处理字符串。

回答by SysAdmin

No, you should not delete the std::stringobjects. they will be automatically freed up when their destructor's gets called.

不,您不应删除std::string对象。当它们的析构函数被调用时,它们将被自动释放。

回答by UncleBens

You don't need a destructor (nor a copy constructor, nor an overloaded assignment operator). That's the whole point of using a class like string that does things for you, rather than going with C-style strings and manual memory management.

您不需要析构函数(也不需要复制构造函数,也不需要重载赋值运算符)。这就是使用像 string 这样的类为您做事的全部意义,而不是使用 C 风格的字符串和手动内存管理。

The compiler produces all the afore-mentioned for you and by default the destructor just invokes the destructors of all members (this happens implicitly even after a user-defined destructor completes). String's destructor takes over from there.

编译器会为您生成所有上述内容,默认情况下,析构函数只会调用所有成员的析构函数(即使在用户定义的析构函数完成之后,这也会隐式发生)。String 的析构函数从那里接管。