我遇到了一些我无法理解的东西.
我有这个代码:
cout << "f1 * f1 + f2 * f1 - f1 / f2 is: "<< f1 * f1 + f2 * f1 - f1 / f2 << endl;
Run Code Online (Sandbox Code Playgroud)
所有"f"都是对象,所有运算符都被重载.
奇怪这是第一计算是的/操作者,则第二*,然后第一*; 在那之后,操作员+和最后,操作员-.
所以基本上,/并*从右到左的工作,并+和-从左至右的操作人员.
我做了另一个测试...我检查了这段代码:
cout << "f1 * f1 / f2 is: " << f1 * f1 / f2 << endl;
Run Code Online (Sandbox Code Playgroud)
现在,第一个运营商是*,然后只有运营商/.所以现在,它从左到右工作.
有人可以帮助我理解为什么方向上存在差异?
10X!
我试图理解我在这段代码上得到错误:(错误是在g ++ unix编译器下.VS正在编译OK)
template<class T> class A {
public:
T t;
public:
A(const T& t1) : t(t1) {}
virtual void Print() const { cout<<*this<<endl;}
friend ostream& operator<<(ostream& out, const A<T>& a) {
out<<"I'm "<<typeid(a).name()<<endl;
out<<"I hold "<<typeid(a.t).name()<<endl;
out<<"The inner value is: "<<a.t<<endl;
return out;
}
};
template<class T> class B : public A<T> {
public:
B(const T& t1) : A<T>(t1) {}
const T& get() const { return t; }
};
int main() {
A<int> a(9);
a.Print();
B<A<int> > b(a);
b.Print(); …Run Code Online (Sandbox Code Playgroud)