将构造函数标记为 __explicitly__ 需要对象类型
我有一个接受 Material
类型的对象的构造函数:
SomeClass::SomeClass( const Material& mat ) ;
但是,Material
允许通过 Vector
进行构造:
Material::Material( const Vector& v ) ;
因此,SomeClass 可以允许通过
Vector
进行构造:
SomeClass m( vec ) ; // valid, since vec is constructed to a Material first,
// then is passed to the SomeClass(Material) ctor
但是,在使用这种类型的 ctor 多次“搬起石头砸自己的脚”之后(在同一项目的不同类中! )我想禁止构建直接通过 Vector
对象获取 SomeClass
的数据,而总是需要传递 Material
。
有办法做到这一点吗?不知何故,我认为这与 explicit
关键字有关。
I have a constructor that accepts an object of type Material
:
SomeClass::SomeClass( const Material& mat ) ;
However, Material
allows construction by a Vector
:
Material::Material( const Vector& v ) ;
Therefore, SomeClass
can allow construction by a Vector
:
SomeClass m( vec ) ; // valid, since vec is constructed to a Material first,
// then is passed to the SomeClass(Material) ctor
However, after "shooting myself in the foot" more than once with ctors of this type (in different classes in the same project!) I want to disallow construction of SomeClass
by Vector
objects directly, instead always requiring a Material
to be passed instead.
Is there a way to do this? Somehow think it has to do with the explicit
keyword.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将Material(const Vector &v) 声明为显式的;这可以防止从一种到另一种的隐式转换。
当然,这并不是对
SomeClass
的具体限制;这是对任何隐式转换实例的全球禁令。You declare
Material(const Vector &v)
to beexplicit
; this prevents implicit conversion from one to the other.This, of course, is not a specific restriction on
SomeClass
; it's a global ban on any instance of an implicit conversion.您无法在不影响从
Vector
透明地构造Material
的能力的情况下执行此操作。如果您使
Material
的构造函数显式
,那么您总是必须编写
Material(v)
才能构造一个实例。这将防止您意外地使用Vector
实例化SomeClass
,但它也会破坏所有计算结果为Vector
的表达式,其中材料
是预期的。这是有道理的,因为通过不显式声明构造函数,您会说“无论上下文如何,
Vector
与Material
一样好”。然后,您不能后退半步并说“哦,好吧,除了构造SomeClass
时”。You cannot do this without interfering with your ability to transparently construct
Material
fromVector
.If you make
Material
's constructorexplicit
then you will always have to write
Material(v)
in order to construct an instance. This will prevent you from instantiatingSomeClass
with aVector
accidentally, but it will also break all expressions that evaluate to aVector
where aMaterial
is expected.This makes sense, because by not declaring the constructor
explicit
you are saying "aVector
is just as good as aMaterial
no matter what the context". You cannot then do a half-step backward and say "oh, well, except when constructing aSomeClass
".将
Material
的构造函数设为explicit
:或者,如果您不想这样做,请作为一点破解并使用
const
正确性作为牺牲,从const Material& 中删除
。您将无法将临时对象传递给const
垫SomeClass
的构造函数(或者可能会牺牲太大的const
实例)。但是,这会阻止您执行您可能想做的SomeClass(Material(v))
操作。所以只能接近自己想要的,但恐怕也不完全可能。Make the constructor of
Material
explicit
:Or if you don't want to do that, as a bit of a hack and with
const
-correctness as a sacrifice, remove theconst
fromconst Material& mat
. You won't be able to pass a temporary object to the constructor forSomeClass
(or aconst
instance which may be too big a sacrifice). However, that keeps you from doingSomeClass(Material(v))
which you may want to do. So you can only get close to what you want, but I'm afraid that it's not completely possible.