Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access private members of a class?

Is it possible to access private members of a class in c++.

provided you don't have a friend function and You don't have access to the class definition

like image 930
yesraaj Avatar asked Dec 02 '22 08:12

yesraaj


2 Answers

You mean using some pointer arithmetic to gain the access ? It is possible but is definitely dangerous. Take a look at this question also: Accessing private members

like image 184
Naveen Avatar answered Dec 04 '22 23:12

Naveen


I think there was some old school trick like this:

#define private public
#include "header.h"
#define private private

But you are strongly discouraged to do this (I've read quickly that the said something about this in the C++ standard) - if you want to read more about this google for "#define private public"

like image 33
bernhardrusch Avatar answered Dec 04 '22 23:12

bernhardrusch