问题很简单,但我找不到解决方案.
class foo
{
public:
operator int()
{
return 5;
}
};
foo* a = new foo();
int b = a;
Run Code Online (Sandbox Code Playgroud)
是否有可能实现这种行为?
Ale*_*ler 11
你不能.转换运算符需要是类的成员,但foo*不是用户定义的类类型,它是指针类型(此外,它int b = *a可以工作).
你能做的最好的事情就是使用一个实用的功能来完成投射.