如何访问类外的私人数据成员? [英] How to access private data members outside the class?

查看:143
本文介绍了如何访问类外的私人数据成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 A类,如下所示: -

I have a class A as mentioned below:-

class A{
     int iData;
};

我不想为上面类A创建成员函数。我也不想继承上面的类A 。我不想更改 iData 的说明符。

I dont want to create member function for above class A. And also i dont want to inherit the above class A. I dont want to change the specifier of iData.

我的疑问: -


  • 如何访问对象的 iData ,例如 obj1 class A

  • 如何更改或操作 iData 对象 obj1

  • How can i access iData of an object say obj1 which is an instance of class A?
  • How can i change or manipulate the iData of an object obj1?

请注意:使用 friend

推荐答案

该成员是私有的,它在类外不可见。这是公共/保护/私有修饰符的整个点。

You can't. That member is private, it's not visible outside the class. That's the whole point of the public/protected/private modifiers.

(你可能使用脏指针技巧,但我的猜测是,你会输入未定义的行为领域很快。)

(You could probably use dirty pointer tricks though, but my guess is that you'd enter undefined behavior territory pretty fast.)

这篇关于如何访问类外的私人数据成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆