如何解决C ++指向对象向量的指针,需要访问属性
| 我有一个称为actorVector的向量,该向量存储actorManager类型的对象数组。 actorManager类具有私有属性,该属性也是GLFrame类型的对象。它具有访问器getFrame(),该访问器返回指向GLFrame对象的指针。 我已经将actorVector的指针传递给一个函数,因此它是一个指向actorManager类型的对象的向量的指针。 我需要将GLFrame对象作为参数传递给此函数:modelViewMatrix.MultMatrix(**GLFrame isntance**);
我目前一直在尝试这样做,但是我没有得到任何结果。
modelViewMatrix.MultMatrix(*(*actorVector)[i].getFrame());
有任何想法吗?
解决方法
假设
MultMatrix
通过值或引用(而不是指针)取ActorManager
,那么您需要这样做:
modelViewMatrix.MultMatrix(*((*actorVector)[i].getFrame()));
请注意,优先级规则意味着以上等同于:
modelViewMatrix.MultMatrix(*(*actorVector)[i].getFrame());
但是,这就是您已经拥有的,因此肯定有某些您没有告诉我们的事情...
, 试试modelViewMatrix.MultMatrix( *(*p)[i].getFrame() );
#include <vector>
using std::vector;
class GLFrame {};
class actorManager {
/* The actorManager class has a private attribute,which is also an
object of type GLFrame. It has an accessor,getFrame(),which returns
a pointer to the GLFrame object. */
private:
GLFrame g;
public:
GLFrame* getFrame() { return &g; }
};
/* I need to pass the GLFrame object as a parameter to this function:
modelViewMatrix.MultMatrix(**GLFrame isntance**); */
class ModelViewMatrix {
public:
void MultMatrix(GLFrame g){}
};
ModelViewMatrix modelViewMatrix;
/* I have a vector called actorVector which stores an array of objects of
type actorManager. */
vector<actorManager> actorVector;
/* I have passed a pointer of actorVector to a function,so its a pointer
to a vector of objects of type actorManager. */
void f(vector<actorManager>* p,int i) {
/* I need to pass the GLFrame object as a parameter to this function:
modelViewMatrix.MultMatrix(**GLFrame isntance**); */
modelViewMatrix.MultMatrix( *(*p)[i].getFrame() );
}
int main() {
f(&actorVector,1);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。