有空測試下vc7.1,vc8和c++ builder 2007,gcc
寫了個測試程序vc7.1下居然能改變值:
#include <vector>
struct stUpdateItem
{
bool _downloadSucceeded;
stUpdateItem() : _downloadSucceeded(false)
{}
};
struct stDownItem
{
stUpdateItem* _pItem;
bool _bPack;
stDownItem(stUpdateItem* item, bool bPack) : _pItem(item),_bPack(bPack)
{}
};
typedef std::vector<stDownItem> tDownItems;
int _tmain(int argc, _TCHAR* argv[])
{
tDownItems downList;
stUpdateItem item1;
stUpdateItem item2;
stDownItem downItem1(&item1,true);
stDownItem downItem2(&item2,false);
downList.push_back(downItem1);
downList.push_back(downItem2);
for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)
{
if(true == it->_pItem->_downloadSucceeded)
{
std::cout << "before change, found!" << std::endl;
}
}
for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)
{
it->_pItem->_downloadSucceeded = true;
}
for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)
{
if(true == it->_pItem->_downloadSucceeded)
{
std::cout << "after change, found!" << std::endl;
}
}
return 0;
}
struct stUpdateItem
{
bool _downloadSucceeded;
stUpdateItem() : _downloadSucceeded(false)
{}
};
struct stDownItem
{
stUpdateItem* _pItem;
bool _bPack;
stDownItem(stUpdateItem* item, bool bPack) : _pItem(item),_bPack(bPack)
{}
};
typedef std::vector<stDownItem> tDownItems;
int _tmain(int argc, _TCHAR* argv[])
{
tDownItems downList;
stUpdateItem item1;
stUpdateItem item2;
stDownItem downItem1(&item1,true);
stDownItem downItem2(&item2,false);
downList.push_back(downItem1);
downList.push_back(downItem2);
for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)
{
if(true == it->_pItem->_downloadSucceeded)
{
std::cout << "before change, found!" << std::endl;
}
}
for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)
{
it->_pItem->_downloadSucceeded = true;
}
for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)
{
if(true == it->_pItem->_downloadSucceeded)
{
std::cout << "after change, found!" << std::endl;
}
}
return 0;
}
執行結果:
after change, found!
after change, found!
after change, found!