uzi*_*nky 3 c++ class this pass-by-reference
我和上课一起玩,并且遇到了这种好奇心
#include <iostream>
#include <Windows.h>
class pint {
public:
pint() { std::cout << "ctor >> " << this << std::endl; };
~pint() { std::cout << "dtor >> " << this << std::endl; };
pint(int x) { std::cout << "set 1. >> " << this << std::endl; };
pint& operator = (const pint& a) { std::cout << "set 2. >> " << this << " | a >> " << &a << std::endl; return *this; };
};
int main()
{
pint * x1 = new pint;
*x1 = 8;
*x1 = 9;
std::cout << "---------------------" << std::endl;
pint * x2 = new pint;
*x2 = 8;
*x2 = 9;
std::cout << "---------------------" << std::endl;
delete x1;
delete x2;
while (!GetAsyncKeyState(VK_RETURN))
Sleep(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
ctor >> 008731E8
set 1. >> 005BF9A7
set 2. >> 008731E8 | a >> 005BF9A7
dtor >> 005BF9A7
set 1. >> 005BF9A7
set 2. >> 008731E8 | a >> 005BF9A7
dtor >> 005BF9A7
---------------------
ctor >> 00873288
set 1. >> 005BF9A7
set 2. >> 00873288 | a >> 005BF9A7
dtor >> 005BF9A7
set 1. >> 005BF9A6
set 2. >> 00873288 | a >> 005BF9A6
dtor >> 005BF9A6
---------------------
dtor >> 008731E8
dtor >> 00873288
Run Code Online (Sandbox Code Playgroud)
为什么:
这里有趣的是,你不仅有一个物体!你生成一些临时的.
*x1 = 8;
Run Code Online (Sandbox Code Playgroud)
类引脚没有operator=(int),但它可以通过int生成一个pint对象.所以构造函数pint(int)被调用.现在可以给出具有新地址的新对象operator(const pint&)
这就是你看到"set1"文本的原因."8"将首先创建一个临时品脱对象,该对象具有新地址.
如果你添加:"魔法"消失了:
pint& operator = (const int a) { std::cout << "set 3. >> " << this << " | a >> " << &a << std::endl; return *this; };
Run Code Online (Sandbox Code Playgroud)
另一种方法是看到你的编译器使用能够进行"不需要的转换"的构造函数生成一个中间临时,你可以创建转换构造函数explicit.
使用:
explicit pint(int x){...}
Run Code Online (Sandbox Code Playgroud)
现在你的编译器会给你一个错误!