我很困惑要返回一个矢量对象,所以请指导我。谢谢。如何返回一个矢量对象?
我的代码:
struct Vector3D
{
float x, y, z;
};
class Vertex
{
public:
std::vector<Vector3D> xyz;
std::vector<Vector3D> Getxyz()
{
return xyz; // what it returns? reference or copy of this object.
}
Vector3D& getVec(int i)
{
return this->xyz[i]; // is it OK?
}
void addVec(Vector3D p)
{
this->xyz.push_back(p);
}
};
void Somefunction()
{
Vertex* p = new Vertex;
p->Getxyz().push_back(Vector3D(0,0,0)); // 1. is it valid and correct?
Vector3D vec = p->getVec(0); // 2. is it valid and correct?
}
怎么可能可能返回“这个对象的地址”?地址类型是指针类型,函数的返回类型不是指针类型。 –
对不起,我的意思是说对象的引用不是地址.. – furqan