private本来是私有变量,外部无法访问的,但是抖个机灵,我们用指向类的指针和在类里面不断偏移我们的指针地址来访问私有成员变量的值。
#include <iostream>
using namespace std;
#include <stdio.h>
class Point{
private:
int x;
int y;
public:
Point(int x,int y){
this->x=x;
this->y=y;
}
int* getthis(){return &this->y;}
};
int main() {
Point p(99,20);
int* a=((int*)(&p));
cout<<*(a)<<' '<<*(a+1)<<endl;//输出99 20
return 0;
}
``1
(adsbygoogle = window.adsbygoogle || []).push({});