How-to return a const std::vector<Object *const>?

后端 未结 5 1349
北恋
北恋 2021-01-25 06:37

I have a class with a container (containing pointer) as a member:

MyClass{
private:
   std::vector _VecMyObjs;
public:
   const std::vector

        
5条回答
  •  渐次进展
    2021-01-25 07:16

    If you want to return a fresh vector, don't make it const. Put the const keyword before the *.

    std::vector MyClass::GetVecMyObj()
    {
        return std::vector(_VecMyObjs.begin(), _VecMyObjs.end());
    }
    

    I omitted the conversion to TRoadSgmt as you didn't specify the inheritance of this class.

提交回复
热议问题