Home > Back-end >  Why the initial value of same Point a, b, c. Through different a, a, a sequence of output, the resul
Why the initial value of same Point a, b, c. Through different a, a, a sequence of output, the resul

Time:09-26

Main function:
# include "stdafx. H"
# include "Point. H"
#include

Int main ()
{
Point a (3, 4);
Point b (3, 4);
Point c (3, 4);

cout<& lt; a<" -- "& lt; & lt; (+ + a) & lt; <" -- "& lt; & lt; (+) & lt; & lt; Endl;//to enforce a rear + +; Then performs is front-facing + +
cout<& lt; (b++) & lt; <" -- "& lt; & lt; (+ + b) & lt; <" -- "& lt; & lt; (b) & lt; & lt; Endl;//the first execution is front-facing + +;
cout<& lt; (c + +) & lt; <" -- "& lt; & lt; (c) & lt; <" -- "& lt; & lt; (c + +) & lt; & lt; Endl;//the first execution is front-facing + +;

return 0;
}
Class:
# # ifndef _Point_h_
# define _Point_h_
# include "stdafx. H"
#include

using namespace std;

The class point
{
Public:
Point (double m, double n);
~ point () {};
Point& The operator + + ();
Point operator + + (int);
Point& Operator - ();
Point operator - (int);
Friend ostream& The operator & lt; & lt; (ostream& O, point& P);


Private:
Double x;
Double y;

};

# endif

The realization of the class:
# include "stdafx. H"
# include "Point. H"
#include
#include
using namespace std;

//function is the implementation of the

Point: : point (double m, double n)
{x=m;
Y=n;
}

Point& Point: : operator + + ()//if the return value is not used, can't to fetch address operations, is just a temporary variable, + + a=5
{
+ + x;
+ + y;
return *this;
}
Point to point: : operator + + (int)
{
Point old=(* this);
+ + (* this);
Return old;
}

Point& Point: : operator, ()
{
- x;
- y;
return *this;
}

Point point: : operator - (int)
{
Point old=(* this);
- (* this);
Return old;
}

Ostream& The operator & lt; & lt; (ostream& O, point& P)
{
O<" (" & lt; & lt; P.x & lt; <" , "& lt; & lt; P.y & lt; <" ) ";

The return o;
}

Output:



Why the initial value of same Point a, b, c. Through different +, + + a, a sequence of output, the results are different????????????????

CodePudding user response:

This is undefined behavior,

Don't write similar code, + + - to write a statement alone,