c++ 通過內存地址判斷對象是否相同

#include <stdlib.h>
#include <stdio.h>
#include <string>


int main()
{
	int testptr = 1;
	long* ptr1 = (long*)&testptr;
	testptr = 2;
	long* ptr2 = (long*)&testptr;
	
	if (ptr1==ptr2)
	{
		printf("%d %d %d %d", *ptr1, *ptr2, ptr1, ptr2);
	}
	return 0;
}

int testptr 可以換成自己的類或其他類型。地址取最大類型表示。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章