Recently in the teaching process, we met a more interesting code:
int main () {
int i = 0;
i = i++;
// i ask if the value of the print, the result is how much?
return 0;
}
This problem is actually related to the internal front and rear operator implementation issues. Bottom-depth assembly language implementation mechanism,
Below we imitate implementation by pre-and post operators to deepen the understanding of front, rear operator realization of the principle of:
First, we use the class MyInt to simulate an integer:
class MyInt{
public:
int value; // value of the actual value
MyInt (int value) {// Initialization easy: MyInt i = 0;
this->value = value;
}
/**
* (This function will be called automatically when I ++) ++ operator override Front
* @Return Front operation returns a reference to the current object
*/
MyInt& operator++(){
* This + = 1; // accumulated taken after
return *this;
}
/**
* (This function will be called automatically when i ++) after overwriting mounted ++ operator
* Note: Due to function overloading is a parameter types to distinguish between, and front, rear and are not operational parameters. In order to address this vulnerability on linguistics, we had to let rear-mounted operator has a parameter of type int (When called, the compiler will silently specify a value of 0 for this parameter int type)
* @Return returns an object of type const
*/
const MyInt operator++(int){
MyInt oldValue = * this; // current object removed
++ (* this); // accumulating (call the above pre-++ overloaded functions)
return oldValue; // return the previous value is removed
}
/**
* + = Overloaded operators, easy assignment: * this + = 1;
*/
MyInt& operator+=(int value){
this->value = this->value + value;
return *this;
}
The above and similar implementations - // Front; - // MyInt & operator ()
Achieved similar to the above - // Rear; - // MyInt UPInt operator ()
};
With the above code, we look at the main function call:
#include <.......>
#include "MyInt.h"
int main () {
MyInt i = 0; // call the parameterized constructor
++ i; // call i.operator ++ () i value of 1
i ++; // call i.operator ++ (0) i is 2
return 0;
}
From the above results view, it is to achieve the increment value of 1
Back to the question we started using the class :( analog implementation)
int main () {
MyInt i = 0;
i = i++;
// i ask if the value of the print, the result is how much?
return 0;
}
First calls i ++, i.e. i.operator ++ (0), the function code is as follows:
/**
* (This function will be called automatically when i ++) after overwriting mounted ++ operator
* @Return returns an object of type const
*/
const MyInt operator++(int){
MyInt oldValue = * this; // Note: oldValue * this is a copy, not a reference
++ (* this); // here is * this accumulation of objects, rather than oldValue
return oldValue; // return oldValue value has not changed, still the initial value
}
So, i.operater after ++ (0) execution, it becomes an expression: i = 0, that is,
void main(){
//1、i = i++;
//2、i.operator+=(i.operator++(0));
//3、i.operator+=(0) -> i += 0
//4、i = 0
}
Results i = i ++ 0 is running on.
Dry notes more attention to micro-channel public number: the old nine school