C++ 理论关于堆清理的约定,建议的构建,这是好的做法吗?
我有另一个理论问题,正如标题所示,它是评估代码的构建。基本上我正在考虑在任何地方使用这个模板。
我使用的是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Boost 为 RAII 风格的堆管理提供了几个实用程序:
你的提案的缺点:
编辑:开始实施的一个好方法是获取Boost。然后在代码中搜索原始指针,并尝试将每个指针替换为
如果完成此操作,您的代码不应包含任何删除代码>不再了。如果你使用make_shared,你甚至可以消除所有
新的
。如果您遇到任何无法自行解决的问题,请访问 stackoverflow.com ...哦等等,您已经知道了;)Boost provides several utilities for RAII-style heap-managment:
Drawbacks of your proposal:
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.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
If this is done, your code should not contain any
delete
s anymore. If you use make_shared, you can even eliminate allnew
s. If you run into any problems that you cannot solve by yourself, check out stackoverflow.com ... oh wait, you know that one already ;)请改用智能指针和 RAII。这不会将所有
删除
集中在一处,而是将它们从代码中删除。如果您需要自己执行任何清理,这就是析构函数的用途,请使用它们,因为这是 C++ 中的约定。Use smart pointers and RAII instead. That will not center all the
delete
s 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++.