简体   繁体   English

需要帮助空转,不知道输出如何

[英]Need help dry running, dont know how output is the way it is

I'm having difficulty dry running this code. 我很难干运行这段代码。 I can understand clearly whats going on till: 我可以清楚地了解到底发生了什么:

Line 8 8号线

Line 8 8号线

Line 9 9号线

Line 7 7号线

Line 8 8号线

Line 1 1号线

but after this things get muddy. 但是之后,事情变得混乱了。 Can someone explain just a little bit please? 有人可以解释一下吗? How are there three Line 7s after the above? 上面的三行7s之后呢? Shouldn't there be two? 不应该有两个吗?

Sorry its a bit long but I'm at a loss to see how I'm not getting it right. 抱歉,它有点长,但是我茫然不知所措。

Code: 码:

#include <iostream>
using namespace std;

class Square {
    friend Square operator -(Square sq1, Square sq2);
    friend ostream& operator<<(ostream& out, Square & s);
private:
int side;
public:
Square(int);
        Square(Square &);
        Square operator+(Square);
        Square operator/(Square);
        Square operator ++(int);
        Square operator--();
        int getSide();
int getArea();
};

Square::Square(int _side){
   cout<<"Line 8"<<endl;                   
side = _side;
}
Square::Square(Square & s){
   cout<<"Line 7"<<endl;
side = s.side;
}

Square Square :: operator +(Square sq){
   Square squareTemp(0);
squareTemp.side = side + sq.side/sq.side - 1;
cout<<"Line 1"<<endl;
return squareTemp-sq-sq;
}
Square Square :: operator /(Square sq){
    Square squareTemp(0);
squareTemp.side = side - 1;
cout<<"Line 2"<<endl;
return --squareTemp;
}

Square Square :: operator ++(int i){
    Square squareTemp(0);
squareTemp.side = side + 2;
cout<<"Line 3"<<endl;
return squareTemp;
}
Square Square :: operator --(){
    Square squareTemp(0);
squareTemp.side = (squareTemp.side++) +side + 1;
cout<<"Line 4"<<endl;
return squareTemp;
}

int Square :: getSide(){
    cout<<"***If this is the last line of output then it is Wrong Result"<<endl;
return side;
}

int Square :: getArea(){
    cout<<"Line 5"<<endl;
side++;
return side * side;
    cout<<"Line 21"<<endl;
}

Square operator-(Square sq1, Square sq2){
    Square squareTemp(0);
squareTemp.side = sq1.side + sq2.side;
cout<<"Line 6"<<endl;
return squareTemp;
}

ostream&operator<<(ostream& out, Square & s){
    out<<"s.side"<<endl;
return out;
}


int main(){
Square s1(3);
Square s2(4);
cout<<"Line 9"<<endl;

Square s = s1 + s2;
cout<<"Side of s:"<<s.getSide() <<endl;
cout<<"Area of s:"<<s.getArea() <<endl;
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s.getSide()<<endl;
system("PAUSE");
}

Output: 输出:

Line 8 8号线

Line 8 8号线

Line 9 9号线

Line 7 7号线

Line 8 8号线

Line 1 1号线

Line 7 7号线

Line 7 7号线

Line 7 7号线

Line 8 8号线

Line 6 6号线

Line 7 7号线

Line 8 8号线

Line 6 6号线

Line 7 7号线

***If this is the last line of output then it is Wrong Result ***如果这是输出的最后一行,则结果错误

Side of s:11 边数:11

Line 5 5号线

Area of s:144 面积:144

s.side s.side

s.side s.side

***If this is the last line of output then it is Wrong Result ***如果这是输出的最后一行,则结果错误

12 12

The line you're getting confused on is bit from operator+ . 您感到困惑的那条线是来自operator+

cout<<"Line 1"<<endl;
return squareTemp - sq - sq;

The sequence of steps is 步骤的顺序是

  1. evaluate squareTemp - sq , which matches Square::operator-(Square sq1, Square sq2) 计算squareTemp - sq ,匹配Square::operator-(Square sq1, Square sq2)
  2. copy-construct argument sq1 from squareTemp - output "Line 7" squareTemp复制构造参数sq1输出“第7行”
  3. copy-construct argument sq2 from sq - output "Line 7" sq复制复制构造参数sq2输出“第7行”
  4. Output "Line 7" (I will explain this in a moment) 输出“第7行”(我将在稍后解释)
  5. int-construct squareTemp in operator- - output "Line 8" int-construct squareTemp in operator- --输出“第8行”
  6. operator- outputs "Line 6" operator-输出“第6行”
  7. Copy-construct the return value of operator- from `squareTemp - output "Line 7" 从“ squareTemp”复制构造operator-的返回值-输出“第7行”
  8. Now we have the second part of (squareTemp-sq) - sq which is Square::operator-(Square sq1, Square sq2) 现在我们有了(squareTemp-sq) - sq的第二部分(squareTemp-sq) - sqSquare::operator-(Square sq1, Square sq2)
    1. The return value is used as sq1 - in theory another copy could be done, but the compiler elides this. 返回值用作sq1理论上可以完成另一个复制,但是编译器将其删除。
    2. The object created in step 4 is sq2 here 步骤4中创建的对象在此处为sq2
  9. (Repeat steps 5,6,7 to output "Line 8" "Line 6" "Line 7" (重复步骤5、6、7以输出“第8行”,“第6行”,“第7行”

The reason that the step 4 is allowed to be created there even though it isn't used until step 8, is because there is no sequence point in the expression: 尽管直到第8步才使用第4步,但仍允许在其中创建第4步的原因是因为表达式中没有序列点:

Square::operator-( Square::operator-(squareTemp, sq), sq )

The compiler must evaluate squareTemp and the first sq before calling the inner operator- . 编译器必须在调用内部operator-之前对squareTemp和第一个sq求值。 However, it is free to evaluate the second sq at any point before the call to the second operator- . 但是,可以在调用第二个operator-之前的任意时间评估第二个sq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM