#include <iosteam>
using namespace std;
Class A
{
int k;
public:
int getK() { return k; }
operator int() { return k; }
};
int main()
{
A a;
cout << a.getK() << " " << int(a) << endl;
}
What’s the difference, and which one should I use? I’m wondering if typecasting returns a reference and getK returns a copy.
The only difference is that typecasting can be implicit.
Note that c++11 allow you to force cast operator to be explicitly called.