I need to access the private members of a local object from a member function. The example explains it better I think. Is there a way to do this without making *a public, or without providing a function specifically for assigning to *a ? This operator+ function may have to allocate and/or deallocate *a for the local object various times.
This post seems to suggest that this should work.
// object.h
class object {
char *a;
...
}
// object.cpp
object object::operator+(object const &rhs) const {
int amount = ...
object local();
// this is ok
this->a = new char[amount];
// this is ok too
rhs.a = new char[amount];
// this is not
local.a = new char[amount];
....
}
My compile error (g++ 4.6.3) is:
error: request for member ‘a’ in ‘local’, which is of non-class type ‘object()’
object local();
is actually a function declaration, not an object definition. Create your variable using:
object local;
Since operator +
is a class member, you have the rights to access private
members, so the issue is due to most vexing parse.