Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a private variable be accessed through its address?

Tags:

c++

Would it be possible for a public function to return a pointer to a private variable in the class. If so / if not, what would happen? would it crash or is there anything highly unsafe about this? Can the pointed data be read or written to? Thanks

like image 843
jmasterx Avatar asked Aug 08 '10 00:08

jmasterx


4 Answers

Yes, a member function may return a pointer (or reference) to a private data member. There is nothing wrong with this except that in most circumstances it breaks encapsulation.

The data member can certainly be read via the returned pointer or reference. Whether or not it can be written to depends on whether the returned pointer or reference is to a const-qualified object (i.e., if you return a const T*, you won't be able to modify the pointed-to T). For example:

class Example
{
public:
    int*       get()             { return &i; }
    const int* get_const() const { return &i; }
private:
    int i;
};

int main()
{
    Example e;

    int* p = e.get();
    int a = *p;    // yes, we can read the data via the pointer
    *p = 42;       // yes, we can modify the data via the pointer

    const int* cp = e.get_const();
    int b = *cp;   // yes, we can read the data via the pointer
    *cp = 42;      // error:  pointer is to a const int         
}
like image 113
James McNellis Avatar answered Nov 18 '22 18:11

James McNellis


Yes it can be returned and it can be read and written to. It is no more or less dangerous than taking the address of any other variable. Public/private/protected are syntactical constructs that are checked at compile time, and they aren't "contagious" or part of the type of something.

like image 44
Logan Capaldo Avatar answered Nov 18 '22 17:11

Logan Capaldo


A private member is no different from a public member, or any other type of instance. So, yes, you can take pointers to them, and return them.

When taking pointers to any instance-based member, you have to be careful that the parent class is not deleted and doesn't go out of scope, unless you take that pointer and make a true copy of the data/object it points to. If it is deleted or goes out of scope, the pointer becomes a dangling pointer, and you can't use it anymore without your app exploding (or working on non-existent objects, and thus making your program do crazy unexpected things, but not crashing).

Design considerations:

Exposing any of your internal implementation details is potentially a violation of encapsulation. However, if you only want to encapsulate HOW the object you're returning is created/retrieved, then this is a reasonable solution. This would allow you to change the class to get the member object some other way (like querying a file, or an internal dictionary) without breaking code that calls these methods.

like image 22
Merlyn Morgan-Graham Avatar answered Nov 18 '22 18:11

Merlyn Morgan-Graham


"would it crash or is there anything highly unsafe about this? Can the pointed data be read or written to?"

private/protected/public have no effect whatsoever at runtime, so they can't influence the program execution or even crash the program. They are checked at compile time and just cause your compiler to throw an error while compiling if there is a violation.

like image 1
Frank Osterfeld Avatar answered Nov 18 '22 19:11

Frank Osterfeld