C++ 不同常数正确性向量上的赋值算子

C++ 不同常数正确性向量上的赋值算子,c++,stl,C++,Stl,我有一个非常简单的方法: void SomeClass::GetListStuff(std::vector<Stuff const *> &listStuff) const { listStuff = m_listStuff; } 如果我将const从ListStuff指针中移除,效果会很好。我还可以在listStuff上调用insert()(不改变常量的正确性),并且它可以工作。谁能解释一下原因吗?我想你应该这样做: void SomeClass::GetList

我有一个非常简单的方法:

void SomeClass::GetListStuff(std::vector<Stuff const *> &listStuff) const
{   listStuff = m_listStuff;   }

如果我将const从ListStuff指针中移除,效果会很好。我还可以在listStuff上调用insert()(不改变常量的正确性),并且它可以工作。谁能解释一下原因吗?

我想你应该这样做:

void SomeClass::GetListStuff(std::vector<Stuff*> &listStuff) const
{   
       listStuff = m_listStuff;   
}
或者更好的方法是公开迭代器:

std::vector<Stuff*>::const_iterator cbegin() const
{   
       return m_listStuff.cbegin(); //return const_iterator (C++11 only)
                                    //in C++03, you can use begin()
                                    //it will work same as cbegin()
}
std::vector<Stuff*>::const_iterator cend() const
{   
       return m_listStuff.cend(); //return const_iterator (C++11 only)       
                                  //in C++03, you can use end()
                                    //it will work same as cend()
}
std::vector::const\u迭代器cbegin()const
{   
返回m_listStuff.cbegin();//返回常量迭代器(仅限C++11)
//在C++03中,可以使用begin()
//它的工作原理与cbegin()相同
}
std::vector::const_迭代器cend()const
{   
return m_listStuff.cend();//return const_迭代器(仅限C++11)
//在C++03中,可以使用end()
//其工作原理与cend()相同
}

自己编写非常量版本。

我认为您应该这样做:

void SomeClass::GetListStuff(std::vector<Stuff*> &listStuff) const
{   
       listStuff = m_listStuff;   
}
或者更好的方法是公开迭代器:

std::vector<Stuff*>::const_iterator cbegin() const
{   
       return m_listStuff.cbegin(); //return const_iterator (C++11 only)
                                    //in C++03, you can use begin()
                                    //it will work same as cbegin()
}
std::vector<Stuff*>::const_iterator cend() const
{   
       return m_listStuff.cend(); //return const_iterator (C++11 only)       
                                  //in C++03, you can use end()
                                    //it will work same as cend()
}
std::vector::const\u迭代器cbegin()const
{   
返回m_listStuff.cbegin();//返回常量迭代器(仅限C++11)
//在C++03中,可以使用begin()
//它的工作原理与cbegin()相同
}
std::vector::const_迭代器cend()const
{   
return m_listStuff.cend();//return const_迭代器(仅限C++11)
//在C++03中,可以使用end()
//其工作原理与cend()相同
}

自己编写非常量版本。

是否有特殊原因导致您不只是通过常量引用返回列表对象<代码>常量std::vector&SomeClass::GetListStuff()常量{return m_listStuff;}并在调用者中重新分配?如果这两者都不起作用,或者您的代码也不起作用,那么也许您应该调查两者的类型,以确保它们是相同的?是否有某些特殊原因导致您不只是通过const引用返回list对象<代码>常量std::vector&SomeClass::GetListStuff()常量{return m_listStuff;}并在调用者中重新分配?如果这两者都不起作用,或者你的代码也不起作用,那么也许你应该调查两者的类型以确保它们是相同的?类型是不同的。。。但是C++不一定隐含地处理const转换吗?我可以直接手动分配指针[使用push_back或insert对向量进行插入,即使它们是不同类型的指针],但直接分配它们不起作用。@Pris:这在这里不起作用
std::vector&
可以转换为
std::vector const&
,但是
std::vector&
不能转换为
std::vector&
。看到区别了吗?类型不同。。。但是C++不一定隐含地处理const转换吗?我可以直接手动分配指针[使用push_back或insert对向量进行插入,即使它们是不同类型的指针],但直接分配它们不起作用。@Pris:这在这里不起作用
std::vector&
可以转换为
std::vector const&
,但是
std::vector&
不能转换为
std::vector&
。看到区别了吗。
std::vector<Stuff*>::const_iterator cbegin() const
{   
       return m_listStuff.cbegin(); //return const_iterator (C++11 only)
                                    //in C++03, you can use begin()
                                    //it will work same as cbegin()
}
std::vector<Stuff*>::const_iterator cend() const
{   
       return m_listStuff.cend(); //return const_iterator (C++11 only)       
                                  //in C++03, you can use end()
                                    //it will work same as cend()
}