我对我认为应该是一个非常简单的操作感到困惑。我调用一个创建std :: vector并返回它的方法。然后我将一个现有的std :: vector设置为该结果,期望=运算符起作用。代码如下所示:
std::vector<BTLECharDecl> charDeclList;
charDeclList.clear();
// get the services on the agent
// It loads the discovered services into the class variable 'serviceList'
if((result = discoverServices()) != rc_SUCCESS)
{
std::cout << "Discovering service failed. Function returned message "
<< eResultCode2str(result) << "(" << hex << result << dec << ")" << endl;
return result;
}
if(serviceList.size() > 0)
{
for(uint16 i = 0; i < serviceList.size(); i++)
{
// Get chararcteristic Declaration List as Objects for each service
startHandle = serviceList.at(i).getServiceData().startHandle;
endHandle = serviceList.at(i).getServiceData().endHandle;
if(startHandle < endHandle)
{
// !! WHY DO I NEED THIS clear()?? !!
charDeclList.clear();
charDeclList = discoverCharDecl(serviceList.at(i).getServiceData().startHandle, endHandle);
}
如果我在调用返回此类对象的方法之前没有调用charDeclList.clear()方法,我将从之前的循环中获取剩余的垃圾。并非总是,但有时候。我本来期望赋值运算符完全替换被调用方法返回的任何东西。我的想法有什么问题? (调用clear()可以解决问题但是它很不稳定。)使用MS Visual Studio 2008。