我很困惑要返回一個矢量對象,所以請指導我。謝謝。如何返回一個矢量對象?
我的代碼:
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