Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way I can access Private member variable of a class?

Tags:

c++

Is there any way I can access Private member variable of a class?

Editing: Not from a member function or friend function but through an instance.

like image 341
Sandeep Avatar asked Feb 08 '10 06:02

Sandeep


People also ask

How do you access private variables in a class?

We have used the getter and setter method to access the private variables. Here, the setter methods setAge() and setName() initializes the private variables. the getter methods getAge() and getName() returns the value of private variables.

Can you access a private variable outside of its class?

Private variable can declare with the private keyword. In the below example, we will see we can access private variable in the same class. In the below example, we will see we cannot access private variable outside the class.

Can you access private variables in the same class?

This is perfectly legal. Objects of the same type have access to one another's private variables. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level (this particular instance of a class).


5 Answers

GotW #76 has fascinating language-lawyery details on how to do some of this stuff. :-)

like image 112
Chris Jester-Young Avatar answered Nov 09 '22 05:11

Chris Jester-Young


Just cast it around, shift memory and cast back. (didn't compile the code, but you should get the idea).

class Bla
{
public:
    Bla() : x(15), str("bla") {}
private:
    int x;
    std::string str;
}

int main()
{
    Bla bla;

    int x = *((int*)(&bla));
    std::string str = *((std::string*)((int*)(&bla) + 1));

    std::cout << x << str;

    return 0;
}

Since this is an interview question, I won't go into why you shouldn't do that. :)

EDIT: Classes with virtual functions will have virtual table pointer somewhere there as well. I'm not sure if & will give you address of vt or address of first data member.

Alignment is 4 by default (right?), so if member you are reading does not align, shift by 2 bytes to get to the next one.

like image 21
Eugene Avatar answered Nov 09 '22 07:11

Eugene


One of the "dirty tricks" of C++ is to do something like:

#define private public
#include "ClassHeader.h"

// now all the private members of the included class are public

I strongly do not recommend that you do this.

like image 33
Greg Hewgill Avatar answered Nov 09 '22 06:11

Greg Hewgill


You could:

  1. Place the private members in the public section
  2. Make your class or function a friend of the class.
  3. Provide an accessor to the data.
  4. Take the address of the class, add the offset to that variable, cast, and dereference. (Yuck)

What are you trying to do? If something is private, don't mess with it. It's private for a reason.

like image 28
GManNickG Avatar answered Nov 09 '22 07:11

GManNickG


Yes. You can access a private member:

  • ...within other instances of the same (exact) type.
  • ...within classes or functions declared to be a friend of that class.
  • ...via a public accessor (getter/setter) member function.
like image 3
Michael Aaron Safyan Avatar answered Nov 09 '22 05:11

Michael Aaron Safyan