"no operator" 运算符,它是什么?

The "no operator" operator, what is it?

什么运算符重载允许我这样做?有没有“无运营商”的运营商?我知道为了获得价值我必须做某种调用(),但我想知道是否有可能清理它。

template<typename T>
class Stuff{
private:
  T stuff{};
public:
  T& operator () (){
  return stuff;
  }
  T& operator = (const T& val){
  stuff = val;
  return stuff;
  }
};

int main()
{
  int myInt = 0;
  Stuff<int> stuff;
  stuff = myInt;
  myInt = stuff(); // <- works
  myInt = stuff; // <- doesn't, is there a way to do it ?
}

是的,有一个方法:在 Stuff 中构建一个 user-defined 转换函数:

operator T() const
{
    std::cout << "I'm here";
    return 0;     
}

因为myIntT类型,所以赋值时会调用这个函数myInt = stuff