overloading [] operator for pointers
Hello to everyone.
I'm trying to overload array-access operator, but I'm not able to make it work with pointers to objects.
I made a simple example to expose the problem:
NumberSet.h
1 2 3 4 5 6 7
|
class NumberSet
{
bitset<9> values;
public:
NumberSet(short startValue);
char operator[](int index);
}
|
NumberSet.cpp
1 2 3 4 5 6 7 8 9
|
NumberSet::NumberSet(short startValue)
{
values = startValue;
}
char NumberSet::operator[](int index)
{
return values[index];
}
|
Example of usage with error: Cannot convert 'NumberSet' to char)
1 2
|
NumberSet* set = new NumberSet(25);
char anElement = set[0];
|
Example of usage without error
1 2
|
NumberSet set(25);
char anElement = set[0];
|
What I'm doing wrong?
When you use a pointer you need to
dereference it:
1 2
|
NumberSet* set = new NumberSet(25);
char anElement = (*set)[0];
|
Last edited on
It worked. Thanks for your reply.
Topic archived. No new replies allowed.