简体   繁体   中英

Breaking encapsulation in C++

Is there any way to break a class enacapsulation? A friend keyword can be used for the same. If i use a friend function or class, then I can access all public and private data members of it. I want to know is there any other way for the same.This is one of my interview question, I have googled much, but not found convincing answers, hope someone could help me. Thanks in advance.

"

I would say that friend does not break encapsulation. See https://isocpp.org/wiki/faq/Friends#friends-and-encap

Now let's ignore the issue of what "encapsulation" actually means. It is generally possible to access private members using a template specialization hack. The basic idea, for when the class has a member template, is explained in http://www.gotw.ca/gotw/076.htm .

This approach can be extended to access any private data member or private member function of a class, even if the class contains no templates. See http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html and http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.html .

It depends what you men by breaking encapsulation. Generally all operations perfomed on class members outside of the class may be considered as breaking class encapsulation.

For example getters that return non-const reference may be considered as breaking encapsulation because they expose private class members in such way, that you can freely modify them and there is little controll over this process.

Yes, there are. If the class has a public template member function, you can specialize it for a type of yours, and do whatever you want inside it.

There is a book by Herb Sutter that discusses this and other possibilities. IIRC, this is the only one that has standard conformance.

Anything you can do in C is also possible in C++ .

You can take a pointer to the class, cast it to char * , treat it as an array and start overwriting the memory with whatever you like, adjusting all the private member variables. You can do this even if someone is using the pImpl idiom to try to hide those variables from you.

Doing things like that is generally a sign of code smell and great evil... the road to hell is paved with good intentions, they say. :)

But in general, when you make a variable or a function private, you are really saying "please don't touch this". The next programmer to come along can always do more or less whatever they want, without modifying your class definition. The features "private" and "public" really are just statements of intent, and a way to inconvenience someone who wants to go against your intent, and a guide to someone who isn't sure where they should be looking for something. It's like the locks on the door to your house -- anyone who is determined can get in anyways.

This is not for the meek and only use in extreme and temporary situations. The following 3rd party header had private<\/code> fields I needed to alter.

#define private public                                                                                         
#include "controllers/incremental_mapper.h"
#undef private

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM