如何使用同一个类的公共成员访问类外的类的私有成员 [英] how to access private members of a class outside the class using public members of the same class

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

问题描述

如何使用同一类的公共模型来访问类外的类变量

how to acces class variables outside the class with the use of public memers of the same class

推荐答案

通过实现get和set函数:

By implementing get and set functions:
class MyClass {
public:
    MyClass();
    int getInt() const { return m_nInt; }
    void setInt(int n) { m_nInt = n; }
private:
    int m_nInt; 
};



如果您有一个无法更改的现有类,请派生您的类并添加get和set函数。


If you have an existing class that can't be changed, derive your class and add the get and set functions.


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

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