[英]Accessing a private field from one class to another not related to the former class in C++ (OOP)
I have 2 classes - A and B
.我有 2 个班级 - A and B
。 Class A
has a member list<int>
and a method addData()
that adds a number to this list.类A
有一个成员list<int>
和一个方法addData()
将一个数字添加到这个列表中。 The second class B
has a function displayContent()
- it iterates through the list of the class A
and prints its content (the added numbers).第二个类B
有一个函数displayContent()
- 它遍历类A
的列表并打印其内容(添加的数字)。 The problem I face is that I cannot access the private
(cannot be changed to public) field list
of class A
from the newly instantiated object (from class B
).我面临的问题是我无法从新实例化的对象(来自类B
)访问类A
的private
(不能更改为公共)字段list
。 I would like to ask how can this be achieved with getters/setters
, or a copy constructor
?我想问一下如何使用getters/setters
或copy constructor
来实现?
In displayContent_a, your identified 'a' is undefined, because within that function or class you have never declared a variable 'a'.在 displayContent_a 中,您标识的“a”未定义,因为在该函数或类中,您从未声明过变量“a”。 A simple solution is to pass an object of type A to the function which determines what A object to display the list of:一个简单的解决方案是将类型 A 的对象传递给函数,该函数确定要显示以下列表的 A 对象:
void displayContent_a(A& a) {
//this uses the a from the parameter
for (auto x = a.a.begin(); x != a.a.end(); x++) { //use a.a because we want to access the list
cout << *x << endl;
}
}
However, this won't be enough, as B can't access private data of A. To fix this, you have to make B a friend of A. This can be done in the following way:但是,这还不够,因为 B 无法访问 A 的私有数据。要解决此问题,您必须让 B 成为 A 的朋友。这可以通过以下方式完成:
class B; //forward declare B so it can be used in friend declarations in A
class A
{
//... your content
friend class B;
}
In good code classes are never entangled.在好的代码中,类永远不会纠缠在一起。 This means that if class B
uses class A
than class A
shouldn't have any knowledge about B
.这意味着,如果类B
使用类A
比类A
不应该有任何的知识B
。 So adding a friendship is just a workaround which makes code entangled.所以添加好友只是一种让代码纠缠不清的变通方法。
It is hard to say what is best approach without knowing more context, but there are couple possibilities.在不了解更多上下文的情况下,很难说什么是最好的方法,但有几种可能性。
You can add such API to A:您可以将此类 API 添加到 A:
class A {
public:
void forEach(std::function<void(int)> f) const {
for(auto x : mData) {
f(x);
}
}
private:
list<int> mData;
};
class B {
public:
void displayContent(std::ostream &out) {
a.forEach([&out](int x){ out << x<< ", "; });
}
private:
A a;
};
Advantage is that you can change lots of thing in A
(for example change list to vector) and you do not have to fix anything in B
.优点是您可以更改A
很多内容(例如将列表更改为向量),并且您不必修复B
任何内容。
There are more possibilities.还有更多的可能性。 Simple getter could solve issue:简单的 getter 可以解决问题:
class A {
public:
list<int> getData() const {
return mData;
}
private:
list<int> mData;
};
To access variable 'a' of class A, it is not possible with current implementation.要访问 A 类的变量“a”,当前实现是不可能的。
Firstly, make class B as the derived class of base class A首先,将B类作为基类A的派生类
Even then, it is not possible to access the private variable of class A from anywhere outside even from derived class.即使那样,也不可能从派生类之外的任何地方访问类 A 的私有变量。
Try to implement by declaring variable as protected.尝试通过将变量声明为受保护来实现。
Another implementation can be by declaring class B as friend of class A. That way, private variables can be accessed from class B另一种实现是通过将类 B 声明为类 A 的朋友。这样,可以从类 B 访问私有变量
A getter is a member function which allows the member attribute List<int> L
to be read from outside of the object. getter 是一个成员函数,它允许从对象外部读取成员属性List<int> L
In class A define:在 A 类中定义:
public:
List<int> getList(){
return L;
}
Now you can add In Class B:现在您可以在 B 类中添加:
A a(constructor args);
List<int>=a.getList();
in the same measure you can use setters to change the value of private attributes eg以同样的方式,您可以使用 setter 来更改私有属性的值,例如
public:
void setList(List<int> data){
L=data;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.