开发者

Get std::list by reference

开发者 https://www.devze.com 2023-02-09 05:29 出处:网络
I have a Poly3D class in C++, which has a sole m开发者_StackOverflow社区ember: list<Point_3>

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

精彩评论

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