Skip to main content
added 1 character in body
Source Link
Rufflewind
  • 2.2k
  • 2
  • 16
  • 19

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.)

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence your question concerns entirely an implementation detail.

As a concrete example,

class A
{
public:
  void fun(int x) { a = this + x; }
  A* a;
};

void foo(A& a)
{
  a.fun(1);
}

This is semantically equivalent to the following:

struct A
{
  A* a;
};
void A_fun(AA* *_this_this, int x) { _this->a = _this + x; }

void foo(A& a)
{
  A_fun(a&a, 1);
}

So it is possible for a compiler to implement member functions in this way (or something similar). How an actual compiler implements it will vary, however.

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.)

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence your question concerns entirely an implementation detail.

As a concrete example,

class A
{
public:
  void fun(int x) { a = this + x; }
  A* a;
};

void foo(A& a)
{
  a.fun(1);
}

This is semantically equivalent to the following:

struct A
{
  A* a;
};
void A_fun(A *_this, int x) { _this->a = _this + x; }

void foo(A& a)
{
  A_fun(a, 1);
}

So it is possible for a compiler to implement member functions in this way (or something similar). How an actual compiler implements it will vary, however.

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.)

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence your question concerns entirely an implementation detail.

As a concrete example,

class A
{
public:
  void fun(int x) { a = this + x; }
  A* a;
};

void foo(A& a)
{
  a.fun(1);
}

This is semantically equivalent to the following:

struct A
{
  A* a;
};
void A_fun(A* _this, int x) { _this->a = _this + x; }

void foo(A& a)
{
  A_fun(&a, 1);
}

So it is possible for a compiler to implement member functions in this way (or something similar). How an actual compiler implements it will vary, however.

added 59 characters in body
Source Link
Rufflewind
  • 2.2k
  • 2
  • 16
  • 19

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.)

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence your question concerns entirely an implementation detail.

As a concrete example,

class A
{
public:
  void fun(int x) { a = this + x; }
  A* a;
};

void foo(A& a)
{
  a.fun(1);
}

This is semantically equivalent to the following:

struct A
{
  A* a;
};
void A_fun(A *_this, int x) { _this->a = _this + x; }

void foo(A& a)
{
  A_fun(a, 1);
}

So it is possible for a compiler to implement member functions in this way (or something similar). How an actual compiler implements it will vary, however.

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.)

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence your question concerns entirely an implementation detail.

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.)

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence your question concerns entirely an implementation detail.

As a concrete example,

class A
{
public:
  void fun(int x) { a = this + x; }
  A* a;
};

void foo(A& a)
{
  a.fun(1);
}

This is semantically equivalent to the following:

struct A
{
  A* a;
};
void A_fun(A *_this, int x) { _this->a = _this + x; }

void foo(A& a)
{
  A_fun(a, 1);
}

So it is possible for a compiler to implement member functions in this way (or something similar). How an actual compiler implements it will vary, however.

added 59 characters in body
Source Link
Rufflewind
  • 2.2k
  • 2
  • 16
  • 19

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.)

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence youtyour question concerns entirely an implementation detail.

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize.

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence yout question concerns entirely an implementation detail.

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.)

Note that the standard says nothing about where this is stored. You aren't allowed to take the address of this because it's not a variable. Hence your question concerns entirely an implementation detail.

Source Link
Rufflewind
  • 2.2k
  • 2
  • 16
  • 19
Loading