其他方式禁止某些 C++类构造除了将构造函数声明为私有之外?
假设我有一个带有一些 const 引用成员变量的类,并且我想禁止某种类型的构造。所以我会声明相应的构造函数私有。当然,构造函数必须初始化类的所有const引用成员变量。然而,这样做会导致代码看起来很奇怪:
class A {
};
class B {
B(const A& a): host(a) {}
private:
B():host(A()) {} // This is ugly and not needed !!
const A& host;
};
除了将构造函数声明为私有之外,还有其他方法可以禁止某种构造类型吗?我不想让编译器为我编写构造函数。
Say I have a class with some const reference member variable and I would like to forbid a certain type of construction. So I would declare the according constructor private. Of course, a constructor must initialise all const reference member variables of the class. Doing so, however, results in odd looking code:
class A {
};
class B {
B(const A& a): host(a) {}
private:
B():host(A()) {} // This is ugly and not needed !!
const A& host;
};
Is there another way to prohibit a certain construction type except than declaring the constructor private? I do not want to let the compiler write a constructor for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单地说,不要定义这个:
也就是说,以下应该执行您想要执行的操作:
这个想法是,如果您定义了一个带有参数的构造函数,那么编译器不会生成默认构造函数。这意味着,上述类的实例不能被默认创建!
C++11 解决方案
在 C++11 中,您可以明确告诉编译器不要生成特定的构造函数,如下所示:
不仅如此。还有更多内容,如下所述:
现在有趣的部分
您还可以有选择地禁用选定类型的构造函数,这使得
删除
更有趣。考虑到这一点,此类的对象不仅可以使用
int
参数创建,还可以使用隐式转换为int
的任何类型创建。例如,现在假设,无论出于何种原因,我不希望
A
类的用户使用char
或class B
创建对象,幸运的是或不幸的是,它可以隐式转换为int
,然后您可以将它们禁用:现在开始:
在线演示:http://ideone.com/EQl5R
错误消息非常清楚:
Simply don't define this:
That is, the following should do what you want to do:
The idea is if you've defined a constructor that takes parameter(s), then the default constructor is not generated by the compiler. That means, instances of the above class cannot be default created!
C++11 solution
In C++11, you can explicity tell the compiler not to generate a particular constructor as:
Not only that. There is more to it, as explained below:
Now the interesting part
You can also selectively disable constructor(s) for selected types which makes
delete
more interesting. Consider this,Object of this class can be created not only with
int
argument, but any type which implicitly converts toint
. For example,Now suppose, for whatever reason, I don't want the users of class
A
to create objects withchar
orclass B
, which fortunately or unfortunately can implicitly convert toint
, then you can disable them as:Now here you go:
Online Demo : http://ideone.com/EQl5R
The error messages are very clear:
把它留下吧。一旦提供自定义构造函数,就不会自动生成其他构造函数(复制构造函数除外)。
如果您想禁止任何构造——最终得到一个只有静态成员的类——您可以简单地声明构造函数为私有,而不是定义 em>它。这样的类在 C++ 中很少有用(因为您无法创建它的实例);我能想到的唯一目的是实现特征类:
然而,这是极其罕见的:特征类在 C++ 中很丰富,但它们从不将其构造函数声明为
private
,只是假设每个人都知道不实例化它们。Just leave it out. As soon as you provide a custom constructor, no other constructor is auto-generated (except for a copy constructor).
If you want to forbid any construction – ending up with a class that has only static members – you can simply declare the constructor as private, and not define it. Such a class is very rarely useful in C++ (since you cannot create instances of it); the only purpose that I can think of is to implement trait classes:
However, this is extremely uncommon: trait classes are abundant in C++ but they never declare their constructors as
private
, it’s just assumed that everybody knows not to instantiate them.我将发布 C++11 解决方案:删除构造函数。
I'll post the C++11 solution: delete the constructor.
正如 Konrad Rudolph 所说:一旦提供自定义构造函数,就不会自动生成其他构造函数(复制构造函数除外)。
因此,其他选项是:
将构造函数声明为私有(以便您不能从类继承),但不提供定义:
或者在 C++11 中,如 R. Martinho Fernandes 所说:
As Konrad Rudolph sayd: as soon you provide a custom constructor, no other constructor is auto-generated (except for a copy constructor).
Therefore, other options are:
Declare the constructor private (so that you can't inherit from your class), but do not provide a definition:
Or in C++11, as R. Martinho Fernandes says: