H
hn.ft.pris
Hi:
I've got the following simple code to test vector<const T>,
#include <vector>
#include <iostream>
using namespace std;
int main( void ){
vector<const int> vec;
const int a = 1;
vec.push_back(a);
cout << vec[0] << endl; // Output 1
vec[0] = 2;
cout << vec[0] << endl; // Output 2
return 1;
}
The code passes compilation on MS VC8, but the output is incorrect, for
the data member of container is const int, so it shouldn't be modified.
I guess there is nothing different between a "vector<int>" and
"vector<const int>" in MS VC8.
But above code fails on GCC 3.4.2. The compiler complains that there is
"assignment of read-only location", why does it happens, what shall I
do if I want to manipulate vector <const T>? Thanks for help.
I've got the following simple code to test vector<const T>,
#include <vector>
#include <iostream>
using namespace std;
int main( void ){
vector<const int> vec;
const int a = 1;
vec.push_back(a);
cout << vec[0] << endl; // Output 1
vec[0] = 2;
cout << vec[0] << endl; // Output 2
return 1;
}
The code passes compilation on MS VC8, but the output is incorrect, for
the data member of container is const int, so it shouldn't be modified.
I guess there is nothing different between a "vector<int>" and
"vector<const int>" in MS VC8.
But above code fails on GCC 3.4.2. The compiler complains that there is
"assignment of read-only location", why does it happens, what shall I
do if I want to manipulate vector <const T>? Thanks for help.