#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums {0,1,2};
vector<int> copy {0};
copy.at(0) = nums.at(0); // nums.at(0) returns a reference
nums.at(0) = -1; // why this line doesn't change copy[0]
std::cout << copy.at(0); // print 0
}
Hi, as code indicates, it looks like changing of nums.at(0) doesn't affect nums.at(0). Does copy.at(0) = nums.at(0);
call the copy-assignment operator a copy? It doesn't make sense to me this line actually do a copy, because copy.at(0)
also returns a reference.
Aucun commentaire:
Enregistrer un commentaire