相关疑难解决方法(0)

在C++中显式调用原始运算符函数

int a, b, c; 

//do stuff. For e.g., cin >> b >> c; 

c = a + b;          //works 
c = operator+(a,b); //fails to compile, 'operator+' not defined. 
Run Code Online (Sandbox Code Playgroud)

这另一方面起作用 -

class Foo
{
 int x; 
public:
 Foo(int x):x(x) {} 

 Foo friend operator+(const Foo& f, const Foo& g)
 {
  return Foo(f.x + g.x); 
 }

};    

Foo l(5), m(10); 

Foo n = operator+(l,m); //compiles ok! 
Run Code Online (Sandbox Code Playgroud)
  • 甚至可以直接调用基本类型的运算符+(和其他运算符)(如int)吗?
  • 如果有,怎么样?
  • 如果没有,是否有一个C++参考措辞,明确表明这是不可行的?

c++ function operator-keyword

7
推荐指数
1
解决办法
3413
查看次数

标签 统计

c++ ×1

function ×1

operator-keyword ×1