Wednesday, April 20, 2011

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

From stackoverflow
  • 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

    Johannes Schaub - litb : that answer on the other question was accepted, but it's incorrect. it's not possible to do so, but if it works it's by pure accident: undefined behavior happened.
    Naveen : Yes, edited the link so that it points to the 'correct' answer.
    Johannes Schaub - litb : oh, nice :) btw i remembered wrongly about that accepted answer. doesn't look too bad imho. but the now linked to answer is better imho. cheers
  • Well I might be talking rubish, but I think you could try to define a "twin" class with same members as the class you want to modify but different public/private modifiers and then use reintepret_cast to cast the original class to yours in which you can access the private members.

    Its a bit hacky ;-)

    A bit of code to explain the idea:

    class ClassWithNoAccess 
    {
    public:
      someMethod();
    
    private:
      int someVar;
    };
    
    class ClassTwin 
    {
    public:
      someMethod();
    
    public:
      int someVar;
    }
    

    and somewhere in the code:

    ClassWithNoAccess* noAccess = new ClassWithNoAccess();
    ClassTwin* twin = reinterpret_cast<ClassTwin *>(noAccess);
    twin->someVar = 1;
    

    edit: so like someone already wrote before, this might work but the standard does not guarantee the order of the variables with public and private modifier will be the same

    David Rodríguez - dribeas : You must be careful if you intend in going that way. Changing your private to public methods can turn a regular class into an aggregate or a POD, effectively changing how the compiler deals with it.
  • I think there was some oldschool 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 a C++ standard) - if you want to read more about this google for "#define private public"

    xtofl : Yuck man that's just plain mean!
    Les : I've not defined a class in two header files. Is it possible to declare public members in one file and private in another and only deliver the public header? If so, we have absolutely no idea what else is in the class.
    bernhardrusch : You could use something like the PIMPL idiom http://www.gotw.ca/gotw/024.htm to hide the private parts
  • See also this question from yesterday http://stackoverflow.com/questions/726096/accessing-private-members

  • Even if it were possible through some nasty hack - see earlier posts - you SHOULD not do it.

    Encapsulation exists for a very good purpose, and setting class member as private means that the developer did not intend anyone to mess around with that member. That should mean

    "You don't have to access this member in order to use the public interface to it's full intended extent"

0 comments:

Post a Comment