A
Adrian
I was trying to create a "readonly" operator[] in public scope and a
"write" operator[] in private scope of the class. But the compiler
seem intent on trying to use the private version.
Am I expecting to much from the compiler or is it not possible for it
to resolve the correct version of the function.
TIA
Adrian
dluadrianc:/home/adrianc> g++ -Wall -ansi -pedantic scope.cc
scope.cc: In function 'int main(int, char**)':
scope.cc:17: error: 'int& Container:perator[](size_t)' is private
scope.cc:31: error: within this context
#include <iostream>
#include <vector>
class Container : private std::vector<int>
{
public:
const int &operator[](size_t i) const
{
return (this->operator[](i));
}
using std::vector<int>::resize;
void set(size_t t, int i)
{
this->operator[](t)=i;
}
private:
int &operator[](size_t i)
{
return this->operator[](i);
}
};
int main(int argc, char *argv[])
{
Container a;
a.resize(10);
a.set(3,3);
std::cout << "Read test\n";
if(a[2]==3)
{
std::cout << "Its 3\n";
}
else
{
std::cout << "Its not 3\n";
}
return 0;
}
"write" operator[] in private scope of the class. But the compiler
seem intent on trying to use the private version.
Am I expecting to much from the compiler or is it not possible for it
to resolve the correct version of the function.
TIA
Adrian
dluadrianc:/home/adrianc> g++ -Wall -ansi -pedantic scope.cc
scope.cc: In function 'int main(int, char**)':
scope.cc:17: error: 'int& Container:perator[](size_t)' is private
scope.cc:31: error: within this context
#include <iostream>
#include <vector>
class Container : private std::vector<int>
{
public:
const int &operator[](size_t i) const
{
return (this->operator[](i));
}
using std::vector<int>::resize;
void set(size_t t, int i)
{
this->operator[](t)=i;
}
private:
int &operator[](size_t i)
{
return this->operator[](i);
}
};
int main(int argc, char *argv[])
{
Container a;
a.resize(10);
a.set(3,3);
std::cout << "Read test\n";
if(a[2]==3)
{
std::cout << "Its 3\n";
}
else
{
std::cout << "Its not 3\n";
}
return 0;
}