Sandeep
Sandeep

Reputation: 3277

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

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.

Upvotes: 9

Views: 3255

Answers (8)

Duncan
Duncan

Reputation: 2094

I think it depends on how the question is phrased:

Q: How would you access a private member variable? A: I wouldn't.

Along with:

Q: How would you implement ... A: I wouldn't, it's already been done, I'd leverage an existing framework/library.

Interviewers don't always want to know if you can make a wheel, sometimes they are probing to see if you know how to find the nearest service station. :)

Upvotes: 0

Eugene
Eugene

Reputation: 7258

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.

Upvotes: 5

Kornel Kisielewicz
Kornel Kisielewicz

Reputation: 57555

Why would you want to?

Visibility rules are clear:

  • private methods are to be accessed only by methods of the class
  • protected methods can be accessed by methods of the class or descendants
  • public methods can be accessed by anyone

... hence -- if you're writing the class yourself, choose the right visibility. If it's a supplied class, thing carefully why it was made private in the first place...

If you decide to break that rule however, you have several options:

  • you can befriend the class that ought to access the private methods via a friend specifier
  • you can use an ugly preprocessor hack that probably someone posted already, but do it only if you need to use the fields or methods to do unit-testing -- any other use is bad design
  • you can use an ugly type-casting hack, but it's so ugly that I'm afraid to even post it not to get downvoted ;>

Upvotes: 2

Conspicuous Compiler
Conspicuous Compiler

Reputation: 6469

While we're proposing bad ideas, there is nothing on the code end which enforces encapsulation -- it's entirely a compiler trick -- so you can write assembly to directly access private members.

But why not just rewrite the base class if it isn't doing what you want already?

Upvotes: 2

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95499

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.

Upvotes: 3

GManNickG
GManNickG

Reputation: 503855

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.

Upvotes: 5

Greg Hewgill
Greg Hewgill

Reputation: 993095

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.

Upvotes: 5

sameer karjatkar
sameer karjatkar

Reputation: 2057

Yes why not , through a member function

Upvotes: 0

Related Questions