我有代码:
我的载体:std::vector<spreadsheet> spreadsheets;
//create a new spreadsheet if it doesn't already exist. Link the session to the spreadsheet.
bool server::createSpreadsheet(session* session, std::string name, std::string password)
{
std::cout << "beginning" << std::endl;
bool found = false;
for(int i = 0; i < spreadsheets.size(); i++)
{
spreadsheet test = spreadsheets.at(i);
std::cout << "spreadsheet name = " << &test << std::endl;
if(spreadsheets.at(i).name == name) //spread sheet already exists
{
found = true;
}
}
std::cout << "After For found = " << found << std::endl;
if(!found) //create a spreadsheet
{
spreadsheet * spr = new spreadsheet(name, password);
spr->linkSession(session);
spreadsheets.push_back(*spr);
}
else
{
std::cout << "in else" << std::endl;
return false;
}
return true;
}
我希望这会每次都创建一个新的电子表格对象found
并将false
其放置在向量中。但是根据我得到的输出,它似乎每次都放同一个对象?我不知道这里发生了什么?
运行多次后输出:
beginning
spreadsheet name = 0x7fff568bce10
spreadsheet name = 0x7fff568bce10
spreadsheet name = 0x7fff568bce10
spreadsheet name = 0x7fff568bce10
spreadsheet name = 0x7fff568bce10
spreadsheet name = 0x7fff568bce10
spreadsheet name = 0x7fff568bce10
spreadsheet name = 0x7fff568bce10
spreadsheet name = 0x7fff568bce10