In this code:
int main()
{
std::vector<int> src{1, 2, 3};
std::cout << "src: ";
for (std::vector<int>::const_iterator x = src.begin(); x != src.end(); ++ x)
{
std::cout << *x << ' ' << &(*x) << std::endl ;
}
std::vector<int> dest(std::move(src));
std::cout << "src: ";
for (std::vector<int>::const_iterator x = src.begin(); x != src.end(); ++ x)
{
std::cout << *x << ' ' << &(*x) << std::endl ;;
}
std::cout << "\ndest: ";
for (std::vector<int>::const_iterator x = dest.begin(); x != dest.end(); ++ x)
{
std::cout << *x << ' ' << &(*x) << std::endl ;;
}
std::cout << '\n';
}
src: 1 0x43ea7d0
2 0x43ea7d4
3 0x43ea7d8
src:
dest: 1 0x43ea7d0
2 0x43ea7d4
3 0x43ea7d8
This make sense because the memory address of dest is now what previously src was.
But when I do:
int main()
{
std::vector<int> src{1, 2, 3};
std::vector<int> dest(src.size());
std::cout << "src: " << std::endl;
for (std::vector<int>::const_iterator x = src.begin(); x != src.end(); ++ x)
{
std::cout << *x << ' ' << &(*x) << std::endl ;
}
std::cout << "\ndest: " << std::endl;
for (std::vector<int>::const_iterator x = dest.begin(); x != dest.end(); ++ x)
{
std::cout << *x << ' ' << &(*x) << std::endl ;
}
std::cout << '\n';
std::move_backward(src.begin() , src.end(), dest.end());
std::cout << "src: " << std::endl;
for (std::vector<int>::const_iterator x = src.begin(); x != src.end(); ++ x)
{
std::cout << *x << ' ' << &(*x) << std::endl ;
}
std::cout << "\ndest: " << std::endl;
for (std::vector<int>::const_iterator x = dest.begin(); x != dest.end(); ++ x)
{
std::cout << *x << ' ' << &(*x) << std::endl ;
}
std::cout << '\n';
}
src:
1 0x41e0140
2 0x41e0144
3 0x41e0148
dest:
0 0x41e0160
0 0x41e0164
0 0x41e0168
src:
1 0x41e0140
2 0x41e0144
3 0x41e0148
dest:
1 0x41e0160
2 0x41e0164
3 0x41e0168
Why is the second case the address being different? I thought std::move only changing the pointer, without touching the memory of the original object
Aucun commentaire:
Enregistrer un commentaire