且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以欺骗访问私人C ++类的成员变量吗?

更新时间:2023-02-15 18:17:04

很多方法这样做 - 所有的人都很糟糕。在C ++中的保护是有目的的,以防止你犯错误。它不是作为一种安全措施。如果你想公开访问,只需公开!


Possible Duplicate:
Accessing private members
Is it possible to access private members of a class?

Is there a good (yes I know this is ugly) way to hack to the private data members of a class?

One brute force approach is to copy the header file and in my copy change private to public. But would there be a better way, say doing

#define private public

or something else?

There are lots and lots of ways of doing this - all of them bad. Protection in C++ is there for a purpose, to prevent you from making mistakes. It is not there as a security measure. If you want public access, just make things public!