C++ 理论关于堆清理的约定,建议的构建,这是好的做法吗?

发布于 2024-09-17 19:56:01 字数 839 浏览 10 评论 0原文

我有另一个理论问题,正如标题所示,它是评估代码的构建。基本上我正在考虑在任何地方使用这个模板。

我使用的是VC++ VS2008(全部包含)

斯塔佩尔.h

class Stapel
{
public:
    //local vars
    int x;

private:
public:
    Stapel();
    Stapel(int value);
    ~Stapel(){}

    //getters setters
    void set_x(int value)
    {
        x = value;
    }

    int get_x(int value)
    {   
        x = value;
    }   

void CleanUp();

private:
};

Stapel.cpp

#include "Stapel.h"

Stapel::Stapel()
{

}

Stapel::Stapel(int value)
{
    set_x(value);
}

void Stapel::CleanUp()
{
    //CleanUpCalls
}

这里的重点是清理方法,基本上我想将该方法放在我的所有文件中,并在需要时简单地让它执行删除调用,以确保它全部放在一个位置,并且我可以防止删除飞翔作为一个菜鸟,我知道这可能不是你想乱搞的东西,也不是马虎堆砌的东西。

这个构建怎么样?

好坏?为什么 ?

那么使用析构函数来完成此类任务怎么样?

I have another theory question , as the title suggested it's to evaluate a build of code. Basically I'm considering using this template everywhere.

I am using VC++ VS2008 (all included)

Stapel.h

class Stapel
{
public:
    //local vars
    int x;

private:
public:
    Stapel();
    Stapel(int value);
    ~Stapel(){}

    //getters setters
    void set_x(int value)
    {
        x = value;
    }

    int get_x(int value)
    {   
        x = value;
    }   

void CleanUp();

private:
};

Stapel.cpp

#include "Stapel.h"

Stapel::Stapel()
{

}

Stapel::Stapel(int value)
{
    set_x(value);
}

void Stapel::CleanUp()
{
    //CleanUpCalls
}

The focal point here is the cleanup method, basically I want to put that method in all my files everywhere , and simply let it do my delete calls when needed to make sure it's all in one place and I can prevent delete's from flying around which , as a rookie, even I know is probably not something you want to mess around with nor have a sloppy heap.

What about this build?

Good bad ? why ?

And what about using destructors for such tasks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆之渊 2024-09-24 19:56:01

Boost 为 RAII 风格的堆管理提供了几个实用程序:

  1. 智能指针(这里针对不同场景有多种实现)
  2. 指针容器

你的提案的缺点:

  1. 在你的实现中,你仍然有记住在每次进行堆分配时在 CleanUp 方法中进行删除。如果您的程序具有任何类型的非线性控制流(某些分配可能仅在某些情况下发生),则跟踪这些分配可能会非常困难。通过将资源(在本例中为内存)的释放绑定到堆栈上对象的生命周期,您不必担心那么多。您仍然需要考虑诸如循环引用之类的事情。
  2. RAII 可帮助您编写异常安全代码。
  3. 根据我的经验,RAII 会带来更加结构化的代码。仅在某个循环或分支内需要的对象不会在其他地方初始化,而是在需要它们的块内初始化。这使得代码更易于阅读和维护。

编辑:开始实施的一个好方法是获取Boost。然后在代码中搜索原始指针,并尝试将每个指针替换为

  1. 引用
  2. 智能指针
  3. 指针容器,如果它是拥有指针的容器

如果完成此操作,您的代码不应包含任何删除代码>不再了。如果你使用make_shared,你甚至可以消除所有新的。如果您遇到任何无法自行解决的问题,请访问 stackoverflow.com ...哦等等,您已经知道了;)

Boost provides several utilities for RAII-style heap-managment:

  1. Smart pointer (there are several implementations here for different scenarios)
  2. Pointer Containers

Drawbacks of your proposal:

  1. In your implementation, you still have to remember to place a delete in the CleanUp-method for every heap-allocation you do. Tracking these allocations can be very difficult if your program has any kind of non-linear control flow (some allocations might only happen under certain circumstances). By binding the deallocation of resources (in this case memory) to the lifetime of objects on the stack, you do not have to worry as much. You will still have to consider things like circular references.
  2. RAII helps you write exception-safe code.
  3. In my experience, RAII leads to more structured code. Objects that are only needed inside a certain loop or branch will not be initialized somewhere else, but right inside the block where they are needed. This makes code easier to read and to maintain.

Edit: A good way to start implementing that is to get Boost. Then search your code for raw pointers, and try to replace every pointer by

  1. A reference
  2. A smart-pointer
  3. A pointer container, if it is a container that owns pointers

If this is done, your code should not contain any deletes anymore. If you use make_shared, you can even eliminate all news. If you run into any problems that you cannot solve by yourself, check out stackoverflow.com ... oh wait, you know that one already ;)

遗弃M 2024-09-24 19:56:01

请改用智能指针和 RAII。这不会将所有删除集中在一处,而是将它们从代码中删除。如果您需要自己执行任何清理,这就是析构函数的用途,请使用它们,因为这是 C++ 中的约定

Use smart pointers and RAII instead. That will not center all the deletes in one place, but rather remove them from your code. If you need to perform any cleanup yourself, that is what destructors are for, use them as that is the convention in C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文