访问专用变量对象 [英] Accessing private variable objects

查看:93
本文介绍了访问专用变量对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个类:

class A {
public:
    A();
   ~A();

    int something();
}

class B {
public:
    B() {a = new A();};
   ~B();
private:
     A *a;
}

main() {
   B x;
}

在main中使用对象a的唯一方法是吸气剂呢?

Is the only way to use object "a" in main is to have a getter for it?

class B {
public: 
    A getA() {return *a;};
private:
    A *a;
}

然后如果我想设置私有变量对象, setter,并设置它在main?

And then if I wanted to set the private variable object, I would need a setter, and set it in main?

编辑:我更新了类A,B,主要一点。主要问题是,如果我在main中创建一个B的对象,最好的方式是什么。我可以访问函数something()反正这样信息可以存储在私有变量a?我知道我不能做xasomething(),因为a是私有的。

I updated class A, B, main a little. Main question is, what is the best way if I create an object of B in main. Can I access the function "something()" in anyway so information can be stored in private variable "a"? I know I can't do x.a.something() because "a" is private.

推荐答案





然后如果我想设置私有变量对象,我需要一个setter,它在主?

同样,答案是:是的。

创建类成员变量 private 的目的是将数据封装到类中,并且通过使用此类的函数成员仅使访问成为可能。最简单的函数成员是 getter setter 成员函数。

它们似乎对于一个天真的观点是很无用的因为你可以简单地使用 public 类成员变量)。但是封装意味着你不需要改变任何东西,如果事后证明,你会需要调查为此成员设置新值,可能需要触发对其他类成员变量的重新计算或任何操作。

The purpose of making a class member variable private is to encapsulate the data into the class, and making access only viable by using function members of this class. The simplest kind of function members are the getter and setter member functions.
They seem to be pretty useless for a naive point of view (at least as you could simply use a public class member variable instead). But encapsulation means you don't need to change anything, if it turns out later, you'll e.g. need to survey setting new values for this member, might need to trigger recalculation of, or any action on other class member variables.

您的类声明应该如下所示(您通常不需要使用 A * 指针)

Your class declaration should look like follows (you don't need to use an A* pointer usually)

class A {
};

class B {
public:
    B() : a_() {}
    const A& a() const { return a_; } // Getter
    void a(const A& newValue) { a_ = newValue; } // Setter
private:
     A a_;
};

并使用

int main() {
    A a;
    B x;

    x.a(a); // Set the value
    a = x.a(); // Get the value
}






扩展您的修改:


To extend on your edit:


我可以访问 something $ c>反正所以信息可以存储在私人变量 a ?我知道我不能做 xasomething()因为 a 是私人的。

"Can I access the function something() in anyway so information can be stored in private variable a? I know I can't do x.a.something() because a is private."

直接从经典的 getter 函数(如上面提到的)访问该函数

You can't access that function directly from the classical getter function (as I had proposed above) like

x.a().something();

因为 something() 成员函数 A

操作你需要声明getter返回一个非const引用(实际上打破了上面提到的封装规则,并且不建议这样做):

To allow manipulation you'll either need to declare the getter returning a non const reference (which actually breaks the mentioned encapsulation rules, and is discouraged because of this):

    A& a() { return a_; } // Getter

或在 B ,正确地委派该调用:

or provide a member function in class B, that delegates that call properly:

class B {
public:
    // like all of my proposal from above ...

    int doSomething() {
       return a_.something();
    }
private:
     A a_;
};

这篇关于访问专用变量对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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