相关疑难解决方法(0)

转换运算符如何在C++中工作?

考虑这个简单的例子:

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++中工作?(即)编译器何时尝试替换转换运算符后定义的类型?

c++ conversion-operator

38
推荐指数
4
解决办法
4万
查看次数

运算符MyClass*()的含义是什么?

我有以下类定义:

struct MyClass { 
   int id;
   operator MyClass* () { return this; }
};
Run Code Online (Sandbox Code Playgroud)

我很困惑operator MyClass* ()上面的代码中的行.有任何想法吗?

c++

4
推荐指数
1
解决办法
157
查看次数

标签 统计

c++ ×2

conversion-operator ×1