如何禁止在 C++ 中使用默认构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8068088/
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-28 17:57:43  来源:igfitidea点击:

How to forbid the use of the default constructor in C++?

c++objectconstructor

提问by Debugger

I don't want to make it possible in my program to create an object without passing arguments to the constructor.

我不想在我的程序中创建一个对象而不将参数传递给构造函数。

Is there a way?

有办法吗?

回答by wkl

When you declare any other constructor, the compiler will not generate the default constructor for you. If you have specified a default no-argument constructor, you can make it private.

当您声明任何其他构造函数时,编译器不会为您生成默认构造函数。如果您指定了默认的无参数构造函数,则可以将其设为私有。

Remember that the compiler can automatically generate each of these 4 member functions for a class.

请记住,编译器可以为一个类自动生成这 4 个成员函数中的每一个。

  • default no argument constructor
  • default destructor
  • copy constructor
  • assignment operator
  • 默认无参数构造函数
  • 默认析构函数
  • 复制构造函数
  • 赋值运算符

But it will not generate a default one if you have declared one yourself, i.e., if you declared a constructor yourself, it will not create the default constructor. If you didn't declare any of the other 3 though, the compiler can generate them.

但是如果你自己声明了一个,它不会生成一个默认的,也就是说,如果你自己声明了一个构造函数,它不会创建默认的构造函数。如果您没有声明其他 3 个中的任何一个,编译器可以生成它们。

edit: Note that this information applies to C++03, but it is different in C++11 as Matthieu M. mentions in the comments. C++11 also allows for the constructor to be explicitly forbidden. See Offirmo's answer.

编辑:请注意,此信息适用于 C++03,但在 C++11 中与 Matthieu M. 在评论中提到的不同。C++11 还允许显式禁止构造函数。请参阅Offirmo 的回答

回答by Offirmo

While the other answers are true, there is a new technique in C++11 for expressing that : deleted default constructor

虽然其他答案是正确的,但 C++11 中有一种新技术可以表达:删除默认构造函数

It allows forbidding a function without depending on any other trick. It also clearly express your intention in the code.

它允许在不依赖任何其他技巧的情况下禁止功能。它也在代码中清楚地表达了您的意图。

class X
{ 
public:
    X(int) {}
    X() = delete; // will never be generated
};

int main()
{ 
    X x; // will not compile;
    X y(1); // will compile.
    return 0;
}

回答by Jerry Coffin

Define a constructor taking the correct argument(s). This will prevent the compiler from defining a default constructor.

定义一个带有正确参数的构造函数。这将阻止编译器定义默认构造函数。

class X { 
public:
   X(int) {}
};

int main(){ 
    X x; // will not compile;
    X y(1); // will compile.
    return 0;
}

回答by Crazy Bear

The reason why you can create an object without passing arguments to the constructor is that you have a default constructor. A default constructor is a kind of constructor which doesn't have a parameter or all parameters having default values. And if you didn't declare any constructor, the compiler will make one for you. So, the solution is very easy. You can declare a constructor with a parameter without default value, then the compiler won't bother to make the constructor to you.

您可以在不向构造函数传递参数的情况下创建对象的原因是您有一个默认构造函数。默认构造函数是一种没有参数或所有参数都具有默认值的构造函数。如果您没有声明任何构造函数,编译器将为您创建一个。因此,解决方案非常简单。您可以使用没有默认值的参数声明构造函数,那么编译器就不会费心为您制作构造函数。

回答by amit

creating a non empty constructor "cancels" the empty default constructor, so if you don't explicitly also create an empty constructor, no objects will be created without giving arguments.

创建一个非空的构造函数会“取消”空的默认构造函数,所以如果你没有明确地也创建一个空的构造函数,那么不提供参数就不会创建任何对象。

回答by Sebastian Negraszus

Create a private default constructor.

创建一个私有的默认构造函数。