0

我已经创建了一个“主”类,我们将其称为 A(Veichle),并且我有两个从 A 继承的类,我们将它们称为 B(Car) 和 C(MC)。我还有一个处理程序,可以将其称为绑定 A、B 和 C 的“D”。然后我有 Form1 类,可以将其称为 E(Visual)

我想在列表框中的可视表单“E”上打印出来自 A 的私有成员

如果我尝试前)

this->listbox1->items->add(X.veichles[i]->getBrand());

它抱怨 veichles 是 D 中的私人成员。

我怎样才能解决这个问题?

4

2 回答 2

0

那我可以回答我自己的问题了。

handler.cpp与 通信的类类中form1.h,您创建一个函数:

void getPersonByIndex(i);

return this->person[i]->getSurName();

然后在form1.h你写:

for(int i=0;i<this->getNrOfPersons;i++)

String^ str = new String(comm.getPersonByIndex(i)); //this conversion was my problem

this->listbox->beginupdate();
this->listbox->items->add(str);
this->listbox->endupdate();
于 2011-02-23T16:47:24.050 回答
0

私有意味着不允许其他类访问。

您应该创建一个公共访问器函数。例如,GetVehicleByIndex(int idx)

您的代码将如下所示:

A* pVehicle = X.GetVehicleByIndex(i);
if (pVehicle) // assuming NULL indicates error
    add(pVehicle->getBrand());
else
    // react on error
于 2011-02-17T23:48:10.703 回答