C++ 为什么显式删除构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13654927/
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
why explicitly delete the constructor?
提问by ash
When/why would I want to explicitly delete my constructor? Assuming the reason is to prevent its usage, why not just make it private
?
我什么时候/为什么要显式删除我的构造函数?假设原因是为了防止它的使用,为什么不直接使用它private
呢?
class Foo
{
public:
Foo() = delete;
};
采纳答案by Luchian Grigore
How about:
怎么样:
//deleted constructor
class Foo
{
public:
Foo() = delete;
public:
static void foo();
};
void Foo::foo()
{
Foo f; //illegal
}
versus
相对
//private constructor
class Foo
{
private:
Foo() {}
public:
static void foo();
};
void Foo::foo()
{
Foo f; //legal
}
They're basically different things. private
tells you that only members of the class can call that method or access that variable (or friends of course). In this case, it's legal for a static
method of that class (or any other member) to call a private
constructor of a class. This doesn't hold for deleted constructors.
它们基本上是不同的东西。private
告诉您只有类的成员才能调用该方法或访问该变量(当然也可以是朋友)。在这种情况下,static
该类(或任何其他成员)的方法调用private
类的构造函数是合法的。这不适用于已删除的构造函数。
Sample here.
样品在这里。
回答by Peter VARGA
why explicitly delete the constructor?
为什么显式删除构造函数?
Another reason:
I use delete
when I want to assure that a class is called with an initializer.
I consider it as a very elegant way to achieve this without runtime checks.
另一个原因:
我用delete
时,我想,以确保一类被称为有一个初始化。我认为这是一种非常优雅的方式来实现这一点,而无需运行时检查。
The C++ compiler does this check for you.
C++ 编译器会为您执行此检查。
class Foo
{
public:
Foo() = delete;
Foo(int bar) : m_bar(bar) {};
private:
int m_bar;
}
This - very simplified- code assures that there is no instantiation like this: Foo foo;
这 -非常简化- 代码确保没有像这样的实例化:Foo foo;
回答by gybacsi
I've met with default ctors declared as 'deleted' in the source code of LLVM (in AlignOf.h for instance). The associated class templates are usually in a special namespace called 'llvm::detail'. The whole purpose there I think was that they considered that class only as a helper class. They never intended to instantiate them; only to use them within the context of other class templates with some metaprogramming tricks that run in compile time.
我遇到过在 LLVM 的源代码中(例如在 AlignOf.h 中)声明为“已删除”的默认构造函数。关联的类模板通常位于名为“llvm::detail”的特殊命名空间中。我认为那里的全部目的是他们认为该课程仅作为辅助课程。他们从未打算实例化它们;只是为了在其他类模板的上下文中使用它们,并在编译时运行一些元编程技巧。
Eg. there's this AlignmentCalcImpl class template which is used only within another class template called AlignOf as a parameter for the sizeof(.) operator. That expression can be evaluated in compile time; and there's no need to instantiate the template -> so why not declare the default ctor delete to express this intention.
例如。有这个 AlignmentCalcImpl 类模板,它仅在另一个名为 AlignOf 的类模板中用作 sizeof(.) 运算符的参数。该表达式可以在编译时计算;并且不需要实例化模板 -> 那么为什么不声明默认的 ctor delete 来表达这个意图。
But it's only my assumption.
但这只是我的假设。