C++中双操作的新操作
问题描述:
只是一个问题:如何为C++基元类型定义一个新操作?C++中双操作的新操作
我已经定义了一个新的类(A)和我已经超载,结果操作*
所以:
A test();
test * 2; //works
2 * test; //doesn't work
答
struct A {
// ...
friend A operator * (A const& lhs, double rhs);
friend A operator * (double lhs, A const& rhs);
};
可能是因为'测试()'声明的函数,而不是一个' A'。 –