c++使用 int 和 char* 重载构造函数
我尝试使用 int
和 char *
重载构造函数。然后使用 0
进行调用时存在歧义。有没有解决方法/解决方案?
CBigInt (unsigned int);
CBigInt (const char *);
问题出在0
线上:
CBigInt a;
// some more code
a *= 0;
感谢您的回答。
I try to overload constructor with int
and char *
. Then there is ambiguity in call with 0
. Is there any workaround/solution for this?
CBigInt (unsigned int);
CBigInt (const char *);
The problem is on the line with 0
:
CBigInt a;
// some more code
a *= 0;
Thanks for answering.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
明确构造函数之一。只有当传递的类型完全匹配时才会使用它。
Make one of the constructors explicit. It will then only be used when passed type exactly matches.
您可以使用“explicit”关键字:
使用此关键字,您必须将参数显式转换为 const char *,否则将执行 CBigInt(unsigned)。
You could use the "explicit" keyword:
Using this, you must explicitly cast the argument to a const char *, otherwise CBigInt(unsigned) will be executed.
“显式”方法是有效的,但对于将来支持开发人员来说可能不直观。
对于这样的情况,在我之前从事的项目中,我们使用静态工厂方法。
我们将有一个私有的默认构造函数并显式初始化静态工厂中的成员。类似于:
这种方法不仅消除了编译器的歧义,而且消除了以后支持您的代码的人的歧义。
The 'explicit' approach works, but may not be intuitive for supporting developers in the future.
For cases like these, in previous projects I've worked on, we used static factory methods.
We would have a private default constructor and explicitly initialize the members in the static factories. Something like:
This approach removes any ambiguity not only for the compiler, but also for the one who will support your code later.
在这种情况下,我还建议使用显式构造函数,因为我认为任意字符串(构造函数采用的)不会对数字进行建模(您的 CBigInt 类模型) 。这种情况正是
explicit
的设计目的。但是,这对于使用直接初始化的情况不起作用
。一般来说,不应该使用显式来解决内部歧义。它应该仅用于禁止从一种类型到另一种类型的转换,而不是比显式构造函数更喜欢另一个构造函数。事实上,新的 C++0x 统一初始化将不会忽略复制初始化上下文中的
显式
构造函数:统一初始化的规则是:两个构造函数都会被考虑,但是如果选择了显式构造函数,初始化格式错误。
文字
0
是一个int
。假设您希望能够接受所有整数类型,您至少需要添加一个int
采用构造函数。您不需要为小于int
的整数类型添加重载,因为这些类型比其他整数转换或指针更喜欢int
。假设您有 int 重载,您还需要为其余整数类型添加重载,如果可用并且您使用它,则添加 long long 和 unsigned long long< /代码>。这样就不会再出现歧义了:In this case I would also recommend an
explicit
constructor, because I think that an arbitrary string (which your constructor takes) does not model a number (which yourCBigInt
class models). Such cases are whatexplicit
was designed for.However, this will not work for cases where direct initialization is used
In general,
explicit
should not be used to resolve internal ambiguities. It should merely be used to forbid the conversion from one type to the other, but not to prefer another constructor over theexplicit
constructor. In fact, the new C++0x uniform initializations will not ignoreexplicit
constructors in a copy initialization context:The rules for uniform initialization is: Both constructors are considered, but if an explicit constructor is chosen, initialization is ill-formed.
The literal
0
is anint
. Assuming you want to be able to accept all integer types, you need to at least add anint
taking constructor. You don't need to add overloads for integer types smaller thanint
, because those types preferint
over other integer conversions or pointers. Assuming you have anint
overload you also need to add overloads for the remaining integer types and, if available and you use it,long long
andunsigned long long
. Ambiguities will then not arise anymore: