如何在函数内更新传递的引用结构?

时间:2014-11-19 02:33:24

标签: c++ struct pass-by-reference

通过函数引用来回传递对象时遇到了一些麻烦。

以下示例显示了一个结构,一个基于该结构的固定数组变量(这些我无法更改)。

我想创建一个输入单个struct元素blnFindIntersection()的函数。在函数期间从该结构中检索值。但是,在该函数结束时,我想返回具有不同元素和值的结构,因此我的主应用程序可以使用更新的结构。

我想这是另一个“参考”问题,而我无法理解它......我做得对吗?我做错了吗?我该怎么做/我能做什么呢?

const int maxobjects = 100 ; 
EXTERN int numobjects ; 
EXTERN struct object {
  shape type ; 
  float size ;
  float position[4] ; 
  float this[4] ; 
  float that[4] ; 
  mat4 transform ; 
  //... etc more values

} objects[maxobjects] ;


bool blnFindIntersection(object &objIntersection)
 //...
 for(int o=0;o<numobjects;o++) {
  obj = &(objects[o]); 
  //... More code, including value retrieval on objIntersection

  //Help here.... Dont know what to do to return the object with updated values from obj..
  objIntersection = &(objects[o]);
  objIntersection = &(obj);
  objIntersection = obj;
 }
}


int main(int argc, char** argv)
{
 object * objNearest;
 bool intersect = false;
 objNearest = &(objects[0]); 

 //... objNearest-> is being used

 intersect = blnFindIntersection(objNearest);

 //... the updated objNearest-> is being used here

 return 0;
}

0 个答案:

没有答案
相关问题