I have a Poly3D
class in C++, which has a sole m开发者_StackOverflow社区ember: list<Point_3>
using namespace std;
struct Poly3D
{
public:
Poly3D(const list<Point_3> &ptList);
private:
list<Point_3> ptList;
};
My question is, how to GET ( in the sense of C#) by reference, so that the same copy with be returned every time I access the Poly3D.ptList
?
If you really want to provide direct access to the list
, then just write a getter that returns by reference:
struct Poly3D
{
public:
...
list<Point_3> &getList()
{
return ptList;
}
...
};
But if you're doing that, you may as well just make the list
member public
!
#include <list>
using namespace std;
struct Point_3{
};
struct Poly3D
{
public:
Poly3D(const list<Point_3> &ptList);
list<Point_3>& GetPointList();
private:
list<Point_3> ptList;
};
list<Point_3>& Poly3D::GetPointList(){
return ptList;
}
int main(void){
list<Point_3> myList;
Poly3D myPoly(myList);
list<Point_3>& myListRef = myPoly.GetPointList();
}
精彩评论