0

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()’
Community
  • 1
  • 1
xst
  • 2,536
  • 5
  • 29
  • 41
  • Looks like you're trying to call the object constructor, right? You dont need the '()' when calling the default constructor, only when you pass arguments to the constructor. – Brady May 02 '12 at 09:58

1 Answers1

3
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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625