将构造函数标记为 __explicitly__ 需要对象类型

发布于 2024-12-20 05:58:02 字数 705 浏览 2 评论 0原文

我有一个接受 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖不如嘢 2024-12-27 05:58:02

您将Material(const Vector &v) 声明为显式的;这可以防止从一种到另一种的隐式转换。

当然,这并不是对 SomeClass 的具体限制;这是对任何隐式转换实例的全球禁令。

You declare Material(const Vector &v) to be explicit; 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.

执手闯天涯 2024-12-27 05:58:02

您无法在不影响从 Vector 透明地构造 Material 的能力的情况下执行此操作。

如果您使 Material 的构造函数显式

explicit Material( const Vector& v ) ;

,那么您总是必须编写 Material(v) 才能构造一个实例。这将防止您意外地使用 Vector 实例化 SomeClass,但它也会破坏所有计算结果为 Vector 的表达式,其中 材料是预期的。

这是有道理的,因为通过不显式声明构造函数,您会说“无论上下文如何,VectorMaterial 一样好”。然后,您不能后退半步并说“哦,好吧,除了构造 SomeClass 时”。

You cannot do this without interfering with your ability to transparently construct Material from Vector.

If you make Material's constructor explicit

explicit Material( const Vector& v ) ;

then you will always have to write Material(v) in order to construct an instance. This will prevent you from instantiating SomeClass with a Vector accidentally, but it will also break all expressions that evaluate to a Vector where a Material is expected.

This makes sense, because by not declaring the constructor explicit you are saying "a Vector is just as good as a Material no matter what the context". You cannot then do a half-step backward and say "oh, well, except when constructing a SomeClass".

此刻的回忆 2024-12-27 05:58:02

Material 的构造函数设为 explicit

explicit Material(const Vector& v) { ... }

或者,如果您不想这样做,请作为一点破解并使用 const 正确性作为牺牲,从 const Material& 中删除 const。您将无法将临时对象传递给 SomeClass 的构造函数(或者可能会牺牲太大的 const 实例)。但是,这会阻止您执行您可能想做的 SomeClass(Material(v)) 操作。所以只能接近自己想要的,但恐怕也不完全可能。

Make the constructor of Material explicit:

explicit Material(const Vector& v) { ... }

Or if you don't want to do that, as a bit of a hack and with const-correctness as a sacrifice, remove the const from const Material& mat. You won't be able to pass a temporary object to the constructor for SomeClass (or a const instance which may be too big a sacrifice). However, that keeps you from doing SomeClass(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文