//
Main.cpp
// use pointers to access private variables in a class
//
Created by won't tell you who I am on 15-7-25.
Copyright (c) year Xuqigang. All rights reserved.
//
#include <iostream>
Using namespace std;
class class1{
Public:
Class1 () {
}
Private:
int a=ten;
int k=;
};
int Main (int argc, const Char * argv[])
{
//Insert code here ...
std::cout <<"Hello, world!\n";
Class1 *p;
Class1 D
p=&d; The address of object D is the address of the first member variable in object d , and at the moment the address stored in p is also the address of the first member variable
int *f= (int *) p; // Pointer Transformation
printf("%d\n", *f); Dereference to get the value of a private member
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + accesses private members with pointers