Why is the conversion of CL
instance to const int&
ambiguous here?
struct CL
{
operator const int&()
{
}
operator int&()
{
}
};
void fnc(const int&)
{
}
int main()
{
CL cl;
fnc(cl);
}
There are two ways:
1). cl.operator const int&()
leads to user-defined conversion
2). cl.operator int&()
leads to user-defined conversion and then qualification conversion (int&
to const int&
)
First way is better than second way, isn't it? I saw Standard, but found nothing.
You can resolve ambiguity by qualifying a member with its class name using the scope resolution ( :: ) operator. The statement dptr->j = 10 is ambiguous because the name j appears both in B1 and B2 .
Access to a base class member is ambiguous if you use a name or qualified name that does not refer to a unique function or object. The declaration of a member with an ambiguous name in a derived class is not an error. The ambiguity is only flagged as an error if you use the ambiguous member name.
Ambiguity in inheritance can be defined as when one class is derived for two or more base classes then there are chances that the base classes have functions with the same name. So it will confuse derived class to choose from similar name functions. To solve this ambiguity scope resolution operator is used “::”.
[‚am·bə′gyü·əd·ē ‚er·ər] (computer science) An error in reading a number represented in a digital display that can occur when this representation is changing; for example, the number 699 changing to 700 might be read as 799 because of imprecise synchronization in the changing of digits.
This is because both conversions are applicable (equally good) in this context. That is, both int&
and const int&
can be bound by const int&
.
The conversion would not be ambiguous, if you had:
void fnc(int&)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With