考虑这个简单的例子:
template <class Type>
class smartref {
public:
smartref() : data(new Type) { }
operator Type&(){ return *data; }
private:
Type* data;
};
class person {
public:
void think() { std::cout << "I am thinking"; }
};
int main() {
smartref<person> p;
p.think(); // why does not the compiler try substituting Type&?
}
Run Code Online (Sandbox Code Playgroud)
转换运算符如何在C++中工作?(即)编译器何时尝试替换转换运算符后定义的类型?
我有以下类定义:
struct MyClass {
int id;
operator MyClass* () { return this; }
};
Run Code Online (Sandbox Code Playgroud)
我很困惑operator MyClass* ()上面的代码中的行.有任何想法吗?