你为什么要把运算符 `new` 设为私有?
Why would you ever make operator `new` private?
我正在使用 OpenSplice DDS,几乎所有 C++ classes(我使用的基本的,如果重要的话我可以提到它们)都重载了 new
运算符私有(以防止用户使用它们)。我不明白,为什么会有人这样做?有人可以提供一些例子来说明这样做的必要性吗?
为什么我需要new
:因为这些class中的大多数没有默认构造函数,我需要稍后在我的通过 unique_ptr
.
实现
简单技巧: 另一方面...我可以很容易地欺骗这个!我可以用另一个 class 包裹这个 class,然后使用 new
就可以了,对吧?因此,我不明白动机,感觉风格很差。有人可以解释一下吗?
编辑:
澄清一下:提供一个无法转义的好例子是一个很好的答案。这对所有看到 new
运算符私有化的人都有帮助。
严格来说你不需要新的。
新的可以禁止强制对象的堆栈分配(与堆分配相反)。考虑:
struct Point {
int x, y;
};
Point * a = new Point{3, 5}; //sizeof(Point) in heap + sizeof(Point *) in stack.
Point b{3, 5}; //sizeof(Point), directly in the stack
因此,如果你删除 operator new(在 C++11 中,如果你想禁止它是正确的方法)或将其设为私有 (pre-C++11),你将强制执行用户协议。
禁止(但不删除)新建和构造函数的另一个原因可能是 return 您的对象工厂的某种智能指针:
class MyClass {
private:
MyClass();
void * operator new(...);
public:
static std::shared_ptr<MyClass> create() {
//check if object in cache...
auto c = std::make_shared<MyClass>();
//Do more stuff maybe
return c;
}
};
如果需要,这将在工厂构造函数中强制执行某些操作,例如使用缓存或任何其他操作,将禁止外部用户也使用 new,但仍允许在 class 内部使用 new 进行分配(如果你删除了 operator new 你就不能再这样做了)。
why would anyone [overload new
operators to be private (to prevent users from using them)]
大概是为了防止分配对象 directly on heap(即动态)。
Could someone provide some examples that show the necessity of this?
我不知道有什么需要这样做。查找此类决策信息的最佳来源是文档。如果没有记录,那么您可以询问开发人员。
我只能猜测。我的猜测是,设计者希望通过忘记释放动态对象,或者尝试销毁或使用 non-existing 个对象,让用户更难犯错。
编辑:这不是官方证明,但标记为提供图书馆的公司员工的用户有 commented on their forum:
The ISO C++ API is designed to have all objects created on the stack rather than the heap as this allows all memory management to happen automatically based on whether objects are still referred to. Copying an ISO C++ object will not incur an overhead as this will just copy the underlying smart pointer resulting in two references to the same object.
Because most of these classes don't have default constructors, and I need to initialize them later ...
这并不一定意味着您需要动态分配。即使 class 可能没有默认构造函数,它可能仍然有一个合理的默认状态,这可以通过将一些规范值传递给构造函数(例如,nullptr
)来实现。
很多嵌入式C++需要保证在系统启动后不进行动态分配。航空、汽车和医疗器械行业经常有这种要求。有关此类编码标准及其背后的合理性,请参见以下链接:
我正在使用 OpenSplice DDS,几乎所有 C++ classes(我使用的基本的,如果重要的话我可以提到它们)都重载了 new
运算符私有(以防止用户使用它们)。我不明白,为什么会有人这样做?有人可以提供一些例子来说明这样做的必要性吗?
为什么我需要new
:因为这些class中的大多数没有默认构造函数,我需要稍后在我的通过 unique_ptr
.
简单技巧: 另一方面...我可以很容易地欺骗这个!我可以用另一个 class 包裹这个 class,然后使用 new
就可以了,对吧?因此,我不明白动机,感觉风格很差。有人可以解释一下吗?
编辑:
澄清一下:提供一个无法转义的好例子是一个很好的答案。这对所有看到 new
运算符私有化的人都有帮助。
严格来说你不需要新的。
新的可以禁止强制对象的堆栈分配(与堆分配相反)。考虑:
struct Point {
int x, y;
};
Point * a = new Point{3, 5}; //sizeof(Point) in heap + sizeof(Point *) in stack.
Point b{3, 5}; //sizeof(Point), directly in the stack
因此,如果你删除 operator new(在 C++11 中,如果你想禁止它是正确的方法)或将其设为私有 (pre-C++11),你将强制执行用户协议。
禁止(但不删除)新建和构造函数的另一个原因可能是 return 您的对象工厂的某种智能指针:
class MyClass {
private:
MyClass();
void * operator new(...);
public:
static std::shared_ptr<MyClass> create() {
//check if object in cache...
auto c = std::make_shared<MyClass>();
//Do more stuff maybe
return c;
}
};
如果需要,这将在工厂构造函数中强制执行某些操作,例如使用缓存或任何其他操作,将禁止外部用户也使用 new,但仍允许在 class 内部使用 new 进行分配(如果你删除了 operator new 你就不能再这样做了)。
why would anyone [overload
new
operators to be private (to prevent users from using them)]
大概是为了防止分配对象 directly on heap(即动态)。
Could someone provide some examples that show the necessity of this?
我不知道有什么需要这样做。查找此类决策信息的最佳来源是文档。如果没有记录,那么您可以询问开发人员。
我只能猜测。我的猜测是,设计者希望通过忘记释放动态对象,或者尝试销毁或使用 non-existing 个对象,让用户更难犯错。
编辑:这不是官方证明,但标记为提供图书馆的公司员工的用户有 commented on their forum:
The ISO C++ API is designed to have all objects created on the stack rather than the heap as this allows all memory management to happen automatically based on whether objects are still referred to. Copying an ISO C++ object will not incur an overhead as this will just copy the underlying smart pointer resulting in two references to the same object.
Because most of these classes don't have default constructors, and I need to initialize them later ...
这并不一定意味着您需要动态分配。即使 class 可能没有默认构造函数,它可能仍然有一个合理的默认状态,这可以通过将一些规范值传递给构造函数(例如,nullptr
)来实现。
很多嵌入式C++需要保证在系统启动后不进行动态分配。航空、汽车和医疗器械行业经常有这种要求。有关此类编码标准及其背后的合理性,请参见以下链接: