开发者

C++ pointer to a vector of objects, need to access attributes

开发者 https://www.devze.com 2023-03-13 12:40 出处:网络
I have a vector called actorVector which sto开发者_C百科res an array of objects of type actorManager.

I have a vector called actorVector which sto开发者_C百科res an array of objects of type 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.

I have passed a pointer of actorVector to a function, so its a pointer to a vector of objects of type actorManager.

I need to pass the GLFrame object as a parameter to this function:

modelViewMatrix.MultMatrix(**GLFrame isntance**);

I've currently been trying to do it as such, but im not getting any results.

modelViewMatrix.MultMatrix(*(*actorVector)[i].getFrame());

Any ideas?


Assuming MultMatrix takes an ActorManager by value or by reference (as opposed to by pointer), then you want this:

modelViewMatrix.MultMatrix(*((*actorVector)[i].getFrame()));

Note that the precedence rules mean that the above is equivalent to:

modelViewMatrix.MultMatrix(*(*actorVector)[i].getFrame());

However, that's what you already have, so there must be something you're not telling us...


Try 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消