是否可以访问类的私有成员?

发布于 2024-07-16 20:11:14 字数 99 浏览 6 评论 0原文

是否可以在 C++ 中访问类的私有成员。

如果你没有朋友 功能并且您无权访问 类定义

Is it possible to access private members of a class in c++.

provided you don't have a friend
function and You don't have access to
the class definition

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城泪 2024-07-23 20:11:14

您的意思是使用一些指针算术来获得访问权限? 这是可能的,但绝对是危险的。 另请看一下这个问题:访问私有成员

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

走走停停 2024-07-23 20:11:14

我认为有一些像这样的老派技巧:

#define private public
#include "header.h"
#define private private

但是强烈不建议您这样做(我很快就读到了 C++ 标准中关于此的内容) - 如果您想阅读更多有关此 google 的“#define”私人公共”

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"

葮薆情 2024-07-23 20:11:14

好吧,我可能在说垃圾,但我认为你可以尝试定义一个“双胞胎”类,其成员与你想要修改的类相同,但具有不同的公共/私有修饰符,然后使用reintepret_cast将原始类转换为你的类可以访问私有成员。

它有点 hacky ;-)

一些代码来解释这个想法:

class ClassWithNoAccess 
{
public:
  someMethod();

private:
  int someVar;
};

class ClassTwin 
{
public:
  someMethod();

public:
  int someVar;
}

以及代码中的某个地方:

ClassWithNoAccess* noAccess = new ClassWithNoAccess();
ClassTwin* twin = reinterpret_cast<ClassTwin *>(noAccess);
twin->someVar = 1;

编辑:就像之前有人写过的那样,这可能有效,但标准不保证具有 public 和 private 修饰符的变量的顺序将会是一样的

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.

Its a bit 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

昨迟人 2024-07-23 20:11:14

即使可以通过一些令人讨厌的黑客来实现 - 请参阅之前的帖子 - 你应该
不这样做。

封装的存在有一个很好的目的,将类成员设置为私有意味着开发人员不希望任何人乱搞该成员。 这应该意味着

“您不必访问此成员即可充分使用公共接口”

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文