Is it possible to access private members of a class in c++.
是否可以在c ++中访问类的私有成员。
provided you don't have a friend function and You don't have access to the class definition
如果您没有朋友功能,并且您无权访问类定义
4 个解决方案
#1
8
You mean using some pointer arithmetic to gain the access ? It is possible but is definitely dangerous. Take a look at this question also: Accessing private members
你的意思是使用一些指针算法来获得访问权限?这是可能的,但绝对是危险的。另请看一下这个问题:访问私人会员
#2
6
I think there was some old school trick like this:
我觉得有一些像这样的旧学校伎俩:
#define private public
#include "header.h"
#define private private
But you are strongly discouraged to do this (I've read quickly that the said something about this in the C++ standard) - if you want to read more about this google for "#define private public"
但是你强烈反对这样做(我很快就读到了C ++标准中关于这一点的说法) - 如果你想阅读更多关于谷歌的信息“#define private public”
#3
2
Well I might be talking rubish, but I think you could try to define a "twin" class with same members as the class you want to modify but different public/private modifiers and then use reintepret_cast to cast the original class to yours in which you can access the private members.
好吧,我可能会说rubish,但我认为你可以尝试定义一个“twin”类,其中包含与要修改的类相同的成员,但是使用不同的公共/私有修饰符,然后使用reintepret_cast将原始类转换为您的类可以访问私人会员。
Its a bit hacky ;-)
它有点hacky ;-)
A bit of code to explain the idea:
一些代码来解释这个想法:
class ClassWithNoAccess
{
public:
someMethod();
private:
int someVar;
};
class ClassTwin
{
public:
someMethod();
public:
int someVar;
}
and somewhere in the code:
在代码中的某个地方:
ClassWithNoAccess* noAccess = new ClassWithNoAccess();
ClassTwin* twin = reinterpret_cast<ClassTwin *>(noAccess);
twin->someVar = 1;
edit: so like someone already wrote before, this might work but the standard does not guarantee the order of the variables with public and private modifier will be the same
编辑:所以像之前已经写过的人一样,这可能会有效,但是标准并不能保证变量的顺序与public和private修饰符一样
#4
2
Even if it were possible through some nasty hack - see earlier posts - you SHOULD not do it.
即使有可能通过一些讨厌的黑客 - 见前面的帖子 - 你也不应该这样做。
Encapsulation exists for a very good purpose, and setting class member as private means that the developer did not intend anyone to mess around with that member. That should mean
封装存在是为了一个非常好的目的,并且将类成员设置为私有意味着开发人员不打算任何人乱搞该成员。那应该意味着
"You don't have to access this member in order to use the public interface to it's full intended extent"
“您不必访问此成员,以便将公共接口用于其完整的预期范围”
#1
8
You mean using some pointer arithmetic to gain the access ? It is possible but is definitely dangerous. Take a look at this question also: Accessing private members
你的意思是使用一些指针算法来获得访问权限?这是可能的,但绝对是危险的。另请看一下这个问题:访问私人会员
#2
6
I think there was some old school trick like this:
我觉得有一些像这样的旧学校伎俩:
#define private public
#include "header.h"
#define private private
But you are strongly discouraged to do this (I've read quickly that the said something about this in the C++ standard) - if you want to read more about this google for "#define private public"
但是你强烈反对这样做(我很快就读到了C ++标准中关于这一点的说法) - 如果你想阅读更多关于谷歌的信息“#define private public”
#3
2
Well I might be talking rubish, but I think you could try to define a "twin" class with same members as the class you want to modify but different public/private modifiers and then use reintepret_cast to cast the original class to yours in which you can access the private members.
好吧,我可能会说rubish,但我认为你可以尝试定义一个“twin”类,其中包含与要修改的类相同的成员,但是使用不同的公共/私有修饰符,然后使用reintepret_cast将原始类转换为您的类可以访问私人会员。
Its a bit hacky ;-)
它有点hacky ;-)
A bit of code to explain the idea:
一些代码来解释这个想法:
class ClassWithNoAccess
{
public:
someMethod();
private:
int someVar;
};
class ClassTwin
{
public:
someMethod();
public:
int someVar;
}
and somewhere in the code:
在代码中的某个地方:
ClassWithNoAccess* noAccess = new ClassWithNoAccess();
ClassTwin* twin = reinterpret_cast<ClassTwin *>(noAccess);
twin->someVar = 1;
edit: so like someone already wrote before, this might work but the standard does not guarantee the order of the variables with public and private modifier will be the same
编辑:所以像之前已经写过的人一样,这可能会有效,但是标准并不能保证变量的顺序与public和private修饰符一样
#4
2
Even if it were possible through some nasty hack - see earlier posts - you SHOULD not do it.
即使有可能通过一些讨厌的黑客 - 见前面的帖子 - 你也不应该这样做。
Encapsulation exists for a very good purpose, and setting class member as private means that the developer did not intend anyone to mess around with that member. That should mean
封装存在是为了一个非常好的目的,并且将类成员设置为私有意味着开发人员不打算任何人乱搞该成员。那应该意味着
"You don't have to access this member in order to use the public interface to it's full intended extent"
“您不必访问此成员,以便将公共接口用于其完整的预期范围”