开发者

Can a private variable be accessed through its address?

开发者 https://www.devze.com 2023-01-10 11:46 出处:网络
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

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 r开发者_高级运维ead or written to? Thanks


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         
}


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.


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.


"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.


The const qualifier won't protect you on those cases. Considering James's response, please try to cast the const int * to int *

int* cp = (int*)e.get_const();

You will still be able to modify.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号