C ++ overload constructor with int and char *

I am trying to overload the constructor with intand char *. Then there is ambiguity in the call 0. Is there any workaround / solution for this?

CBigInt (unsigned int);
CBigInt (const char *);

The problem is the line with 0:

CBigInt a;
// some more code
 a *= 0; 

Thanks for answering.

+5
source share
4 answers

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

CBigInt (unsigned int);
explicit CBigInt (const char *);
+10
source

You can use the "explicit" keyword:

explicit CBigInt(const char *);

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

+6
source

"" , . , , , factory. . - :

class CBigInt {
public:
...
static CBigInt fromUInt(unsigned int i) { 
    CBigInt result; 
    result.value=i; 
    return result; 
}
static CBigInt fromCharPtr(const char* c) { 
    CBigInt result; 
    result.value=parse(c); 
    return result; 
}
...
private:
CBigInt () {}
/*some internal type here*/ value; 
};

, , .

+2

explicit, , ( ) ( CBigInt). explicit.

,

struct A {
  CBigInt n;
  A():n(0) { } // ambiguity again
};

explicit . , , explicit. , ++ 0x explicit :

CBigInt f() {
  return { 0 }; // still ambiguous
}

CBigInt b = { 0 }; // still ambiguous

: , , .

0 int. , , int. , , int, int . , int, , , long long unsigned long long. :

CBigInt (int);
CBigInt (unsigned int);
CBigInt (long);
CBigInt (unsigned long);
// CBigInt (long long);
// CBigInt (unsigned long long);
explicit CBigInt (const char *);
+1

All Articles