Operator and Casts

So here's my problem.
In my class, I have got an addition operator '+' and a long long int cast
It's something like
1
2
3
4
5
6
7
8
9
10
friend BigNumber operator + (BigNumber& n1, BigNumber& n2)
    {
        //Some code here
    }

//And the cast is as follows
 operator unsigned long long int ()
    {
        //Some code here too..
    }


So my problem is, while doing addition, how am I to distinguish whether the cast is called and operated or the operator is called? And if I want to call the operator and not the cast, how to do so? And if I want to cast rather than call the operator??
Last edited on
an example:
1
2
3
BigNumber a,b;
unsigned long long int c;
c=a+b; //this 


At the commented row, the compiler operate in this way: c = (unsigned long long int)(a.operator+(b));

becase the first it call the operator + to sum a and b, then check int the assignment if type match to left operand, if not check the appropriated casting for right operand, in this case the operator unsigned long long int int your own class.
And if the compiler found 2 possible 'paths' for the operation between two types because of the various arithmetic and conversion operators overloads, it would tell you by an 'ambiguous operator' error at the compilation. Then you would have to tell it which to use, by removing the unwanted operator (if the rest of the coe doesn't use it), or by explicitly casting the operands in the types of the operator you want to use.
Topic archived. No new replies allowed.