Reputation: 23
Could anyone tell me if the pointer "this" in object of class occupy memory when it was created?
class Temp {
private:
Temp &operator=(const Temp &t) { return *this; }
}
Upvotes: 2
Views: 79
Reputation: 364
No, "this" is already by itself a memory reference and hence it would not occupy more memory than the object already does.
Upvotes: 0
Reputation: 66431
this
is the address of the object whose member function is being called, and it doesn't need to be stored anywhere.
It is usually implemented by passing its value as a "hidden" argument to the member functions, so it occupies memory in the same way as any other function argument occupies memory.
The "object-oriented" code
struct A
{
int f() { return this->x; }
int x;
};
int g()
{
A a;
return a.f();
}
will usually be implemented like this "non-object-oriented" code:
struct A
{
int x;
};
int Af(A* self)
{
return self->x;
}
int g()
{
A a;
return Af(&a);
}
Upvotes: 6