Why is it wrong to access the fun()
function this way?
I get this error:
“C++ Operators -> and ->* Applied to Non-Pointer Types”
I can’t understand this error!
class a
{
public:
class b
{
struct c
{
void fun() {};
};
c _c[5];
c* operator[](int count) {
return &_c[count];
}
};
b* _b;
static a* _This;
a();
};
a::a()
{
_This->_b[1]->fun();
}
3
This can be understood by working out the types involved.
_This
has type a*
, so
_This->_b
has type b*
, so
_This->_b[i]
has type b&
. Note that []
is the built-in operator[]
not the b::operator[]
you defined because _This->b
is a pointer. Maybe this is your misunderstanding, b::operator[]
only applies to b
values not b
pointers.
Since _This->b[i]
is not a pointer it is illegal to apply built-in operator->
to it.
2