Fri Nov 07, 2008 6:08 pm
I got a question. I have a class B that has this method:
- Code:
operator int()
{
std::cout << "int()" << std::endl;
return i;
}
I dont know exactly how it works but every time I call this method:
- Code:
int foo1(B b)
{
return b;
}
it prints int().
Fri Nov 07, 2008 6:09 pm
Implicit class conversion to another type using operator overloading.
The C++ compiler makes a best-guess effort at creating a successful
compile by looking at the existing class methods and the type the code
is expecting and then creating the necessary code as output. This is,
IMO, one of the coolest features of C++ (you can even "typecast" between
classes!) but, occasionally, one of the more annoying ones should you
depend on the feature too much. That is, you can "see" a clear path to
the implicit solution but the compiler insists there isn't one.
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.