Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ overload constructor with int and char*

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.

like image 248
ryskajakub Avatar asked Mar 31 '11 11:03

ryskajakub


2 Answers

Make one of the constructors explicit. It will then only be used when passed type exactly matches.

CBigInt (unsigned int);
explicit CBigInt (const char *);
like image 112
Erik Avatar answered Sep 21 '22 01:09

Erik


You could use the "explicit" keyword:

explicit CBigInt(const char *);

Using this, you must explicitly cast the argument to a const char *, otherwise CBigInt(unsigned) will be executed.

like image 43
mfontanini Avatar answered Sep 24 '22 01:09

mfontanini