How can I access the private data memebers of a class, other than using a function.
And please give me an example.

From where else would you want to access or refer to objects other than for declarations and definitions. Static data members of course have to be defined somewhere outside of any function.

OK so you could potentially access a data member of a global or static instance of a class to pass to the initialisation of another global or static object instance, however this is tricky as the order of initialisation of such objects is not defined so you might pass an un-initialised object ...

However if that member you wished to access were private then that would not be allowed, that is what private implies, only other members of the class can access private members of that class.

Did you mean other than a member function of the class in which the private data members are defined?

In which case you cannot, again this is what private implies.

At least not directly. You could access the member by back door methods such as by its offset from the start of an instance (assuming instance data members) of the class, however these are dodgy and often fragile solutions.

You could of course implement a public (or protected) getter member function which returned a reference to the private member, if that reference were not const then you could even modify the value of the private data member, which would probably negate most of the advantages of using private members in the first place:

class A
       explicit A( int data ) : iPrivateData(data) {}
         A()          : iPrivateData(0) {}

       int & getPrivateMemberAccess()
       { return iPrivateData; }

       int   iPrivateData;

Of course most uses of such a function would be to call it from within another function:

void someFunction()
       A a;
       a.getPrivateMemberAccess() = 10;

You could then use this with global variables:

A gA(10);

int gI( gA.getPrivateMemberAccess() );

However heed my warning about sequence of initialisation of global and static data - if gI is initialised before gA then the value of gI will not be 10 as expected - this is especially true if the globals are defined in different source files - and the order may change at any time so it may appear to work for a while then suddenly break as code is changed, or not work with some other compiler.

Of course global data should preferably not be used at all, and at least kept to an absolute minimum otherwise disparate parts of the code become coupled together which makes the code more fragile and harder to get right and maintain as a project grows.

Oh, and behind the scenes the initialisation is performed by code written by the compiler for you...

I expect there may be other silly examples. However life is too short. Try and think of a few for yourself if you are really than interested.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.