tags:

views:

117

answers:

5

Is it possible to declare a pointer to private data member of a class? If so, how do you do it?

+1  A: 

Yes it's possible. You'd have to return the pointer (or reference) from within the context of the class though, or someone who has friend access to the class. That is because the variable itself is private and so can't be accessed otherwise.

class C
{
public:
    int* getXPointer()
    {
        return &x;
    }

    int& getXReference()
    {
        return x;
    }

private:
    int x;
};


int main(int argc, char* argv[])
{
    C c;
    int* p = c.getXPointer();
    int& r = c.getXReference();
    assert(p == &r);
    return 0;
}
Brian R. Bondy
A: 

Yeah, if I understand you right, you'd like to be able to return a pointer to a private member of a class?

private:
        int hidden;
    public:
        int& unHide ()
        {
            return hidden;
        }
xil3
Why have you specified `mutable`?
Billy ONeal
My bad, updated it.
xil3
+8  A: 

Yes, and the same way you would create any other pointer. The catch is, of course, that since the member is private, you can only create the pointer inside the class, where you can see the member.

class A 
{
  public:
    int* getFooPtr()
    {
       return &foo;  // OK; Inside the class foo is visible
    }

  private:
    int foo;
};

int main()
{
   A a;

   int* p_foo1 = &a.foo; // Illegal; Outside the class, foo is private
   int* p_foo2 = a.getFooPtr(); // OK; getFooPtr() is a public member function
}

So it's possible to create pointers to private members, but only inside the class' member functions, and it is possible to return those created pointers from member functions. Whether or not it's a good idea to return pointers to private members is another question entirely (usually it's not a good idea).

Tyler McHenry
Mostly correct, except for "only inside the class' member functions" ... `friend` s can expose object internals too :)
Billy ONeal
... as well as static member can be initialized to point to a private member of the class, which is not inside any function.
AndreyT
+2  A: 

Yes, it is possible, as the previous answers have demonstrated. But as Tyler McHenry asks, "Is it a good idea?". No, it isn't. The member variables are declared private for a good reason, and subverting the encapsulation in this way will only lead to trouble.

Brian Hooper
Whether it's a good idea depends on the scenario. I think people would be quite unhappy if they couldn't get pointers into a std::vector.
Ben Voigt
Hmm. I'd always thought of vectors as containers, but you are right.
Brian Hooper
Declaring a pointer to a private member does not necessarily subvert incapsulation. The only chance when some subversion can happen is when the pointer is more accessible than the member. Nothing in the OP suggests that to be the case.
AndreyT
A: 

The wording of your question is rather confusing. When someone says "declare a pointer to something", they are usually talking about type-related attributes of a pointer, as in "declare a pointer to int". The property of being private does not affect the type of a member at all, meaning that a member of type int is always just a member of type int, regardless of whether it is public or private. This immediately means that a pointer of type int * can always be made to point to a public member of type int, or to a private member of type int. It doesn't matter at all whether the member is public or private.

Another ambiguity in the wording is that in C++ there are ordinary pointers (like int *) and there are pointers of "pointer-to-member" type (like int MyClass::*). When you say "a pointer to data member of a class", it is not clear what kind of pointer you are talking about: ordinary or pointer-to-member. However, the above still applies to both kinds: both can easily point to public, protected or private members of the class. Privateness makes no difference.

Again, the property of being "private" does not affect the type of the member, it only affects its accessibility when referred directly, by name. So, in order to make your pointer to point to a private data member of a class you have to initialize that pointer (or assign to it) in an area where that private data member is accessible: inside a method of the owning class or inside a friend function.

AndreyT