对象数组中每个对象元素的成员如何引用?

来源:百度知道 编辑:UC知道 时间:2024/09/24 22:07:30
比如:CArray<Cobarray,Cnode*> m_node
(这里的Cnode是我自己定义的一个类,里面有个方法叫Func())

我是这样调用的
m_node[0]->Func()
可是报错了“type 'CObArray' does not have an overloaded member 'operator ->'”
还有“left of '->Func' must point to class/struct/union”

应该怎样调用啊??谢谢大家了

数组中
你可以保存Cnode类定义的对象
也可以保存Cnode类定义的对象的指针
如果保存的是对象
调用方式m_node.GetAt(0).Func();
如果保存的是对象的指针
调用方式m_node.GetAt(0)->Func(); //对象没有被释放
建议你在数组中保存对象
我以前遇到过这样的问题
可以这样做
CArray<Cnode,Cnode&>m_array;
Cnode code;
m_array.Add(code);
m_array.GetAt(0).Func();