-8

I've got a class named Area and when I create an Area object I need to keep its address. So in Area's constructor I use the following command:

Area *p = this->Area;

and I get an error saying:

"invalid use of Area::Area".

Any idea of what's going wrong?

Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
Theo K
  • 51
  • 3

2 Answers2

1

this is already a pointer to that object. So you should make something like this:

Area *p = this;

The this pointer is an implicit parameter to all member functions (non-static members). Therefore, inside a member function, this may be used to refer to the invoking object.

granmirupa
  • 2,780
  • 16
  • 27
  • what about static member functions? – Biruk Abebe May 08 '16 at 12:19
  • @bkVnet he ask for store the address that object. Static member functions aren't stored into the object. What's your question? – granmirupa May 08 '16 at 12:22
  • does non-static member functions stored in an object? I though they were part of the class and are shared between all object of the class, that is why we need to send the `this` pointer implicitly to these functions right? – Biruk Abebe May 08 '16 at 12:24
  • 2
    @bkVnet Constructors aren't _static member functions_. – πάντα ῥεῖ May 08 '16 at 12:27
  • @ πάντα ῥεῖ I was referring to the part saying *"..The `this` pointer is an implicit parameter to all member functions"* – Biruk Abebe May 08 '16 at 12:28
  • @bkVnet you mean that:"to all member functions" suggests also static member? – granmirupa May 08 '16 at 12:43
  • yes, that is what i meant, the reason i asked that is, you can't use `this` inside a static member function. – Biruk Abebe May 08 '16 at 12:45
  • @bkVnet ok, I'll edit. Thank's for your clarification – granmirupa May 08 '16 at 12:45
  • @bkVnet: _No_ member functions are "stored in an object". – Lightness Races in Orbit May 08 '16 at 14:00
  • @LightnessRacesinOrbit Really? i didn't understand it that way. I have also read an [answer here](http://stackoverflow.com/questions/15572411/where-are-member-functions-stored-for-an-object) that says otherwise. And if they are stored in an object what is the point of passing `this` to the function. – Biruk Abebe May 08 '16 at 14:04
  • @bkVnet: The very first sentence in the accepted answer is _"Member functions or pointers to them aren't stored in the object"_. The other answer doesn't claim otherwise, either. – Lightness Races in Orbit May 08 '16 at 14:05
  • @bkVnet: _"if they are stored in an object what is the point of passing `this` to the function"_ There wouldn't be one, which is yet more evidence that they're not! – Lightness Races in Orbit May 08 '16 at 14:06
  • @LightnessRacesinOrbit yes,that is what i said here in the comment. – Biruk Abebe May 08 '16 at 14:06
  • @bkVnet: I'm confused. You first asked "_does non-static member functions stored in an object?"_ I revealed to you that no member functions are stored in an object (be they static or otherwise!). Now you appear to be arguing the same thing. Odd. – Lightness Races in Orbit May 08 '16 at 14:07
  • @granmirupa: It's actually not ambiguous because (a) I _didn't_ put a comma there, and (b) if there had been one there then the usage of "an" would have been incorrect. Oh well. The "no" is an important part of the message, as the entire purpose was to emphasis that it's not only non-statics that do not form part of the object in memory, but _all_ member functions hold the same property. – Lightness Races in Orbit May 08 '16 at 14:12
  • @LightnessRacesinOrbit OK, we seem to be on the same page here. In that comment I've also mentioned that i think they aren't "stored in the object". Anyway sorry for being odd! – Biruk Abebe May 08 '16 at 14:12
0

I create an Area object I need to keep its address.

Both self-reference and self-ptr may be initialized in the initializer list of the ctor. (and it is also easy to do in a method)

class Area
{
private:
    Area& selfRef;  // size is 8 bytes
    Area* selfPtr;  // size is 8 bytes - but why bother, just use this
    char  data[1000]; // size is 1000 bytes

public:
   Area() : selfRef(*this), selfPtr(this)
      {
          for (int i=0; i<1000; ++i) data[i] = 0; // initialize data  
      }; 

   void foo() {
      // easy to do in a method:
      Area& localSelfRef = *this;  // 8 bytes
      // ...
      localSelfRef.bar();  // instead of ptr
      this->bar();         // direct use of this ptr
      selfRef.bar();       // use of class initialized selfRef
   }

   void bar() { 
      // ...
   }

}

Class size is 1000+ bytes.

selfPtr and SelfRef and localSelfRef are each 8 bytes (on my system).

2785528
  • 5,438
  • 2
  • 18
  • 20