At the risk of being down-voted by language lawyers, here is a bit of hacker's advice for you in hope that it might be interesting. Private members are not meant to be accessed from outside of the class and class's friends. However, if you absolutely must access those member fields of the class, a hack like this could work:
#include <iostream>
class vivek {
int i;
float d;
public:
vivek() : i(4), d(4.44) {}
~vivek() {}
};
int main()
{
vivek viku;
struct badhack {
int i;
float d;
} *h = (sizeof(badhack) == sizeof viku ? (badhack*)&viku
: ((badhack*)((char*)&viku + sizeof(void*))));
std::cout << "i=" << h->i << ", d=" << h->d << std::endl;
}
Note the game with sizeof's — that is just an example of determining a virtual table that takes sizeof(void*)
bytes and is a first implicit field in the class shall there be any virtual members. If you do not do that and a class happens to have a virtual table, then data offsets will get screwed up and the trick won't work, so that's why we adjust offset by sizeof(void*)
bytes in order to avoid that problem. Now, this is not defined by the standard and is compiler-specific, but I have never ran into a compiler that implements virtual tables differently as this is the most efficient way.
Another option would be as simple as this:
#include <iostream>
#define class struct
#define private public
class vivek {
int i;
float d;
public:
vivek() : i(4), d(4.44) {}
~vivek() {}
};
int main()
{
vivek viku;
std::cout << "i=" << viku.i << ", d=" << viku.d << std::endl;
return 0;
}
Note, however, that above most likely will not work for member functions. Compilers these day are smart and mangle function names differently depending on their access level.
And please do not use tricks like these unless it is necessary to save someone's life and you are willing to sacrifice yours in return.