class A
{
public:
int m_count;
int m_values[300];
void Method1();
}
class B
{
void Method2();
}
void A::Method1()
{
for(int i=0; i<m_count; i++)
{
creates instances of class B
}
}
B :: Method2()对于类B的每个实例都需要类A的m_values数组。这里,类A中的另一个函数每分钟更新一次m_values。 是否可以在类B的所有实例中访问类A的m_values?
答案 0 :(得分:2)
class A {
private:
Type m_values[300];
public:
Type getValue(int index) { return m_values[index]; }
};
class B {
private:
A &a;
public:
void Method2() { /* can call a.getValue(<number>); */ }
B b(A &a) : a(a) {}
};
void A::Method1()
{
for(int i=0; i<m_count; i++) {
creates instances of class B
B b(*this);
}
}