Questions tagged [this-pointer]

The "this" pointer is a compiler-generated pointer during a function call that points to the object upon which that function gets called.

Multiple objects of the same class have identical data members in them. Whenever a function gets called on an object, only the data members of that particular object are changed. The compiler knows which object's data members to access and modify by use of a special pointer, known as the this pointer, which stores the object's address in memory.

Whenever a function gets called on an object, the compiler automatically creates the this pointer and sends it to the function. Because of this, there is no need to explicitly declare or pass this to the function.

this is a keyword in the C++ language and is used in conjunction with the arrow member operator -> when used within a class to access its data members and functions.

void myClass::myFunction()
{
  this->myVariable = 10;
}

Which is equivalent to:

void myClass::myFunction()
{
  myVariable = 10;
}

Sometimes, the object itself is returned using the this pointer:

void myClass::myFunction()
{
  ...
  return (*this);
}
139 questions
4
votes
2 answers

Replacing std::function from within itself (by move-assignment to *this?)

Is it possible to replace one std::function from within itself with another std::function? The following code does not compile: #include #include int main() { std::function func = []() { std::cout <<…
j00hi
  • 5,420
  • 3
  • 45
  • 82
4
votes
7 answers

Is it okay to use the this pointer?

Possible Duplicates: Is there any reason to use this-> When should this-> be used? When should I make explicit use of the this pointer? When working with pointers to classes, I like to add a this-> in front of variables in a class to make it…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
4
votes
4 answers

Why is it legal to pass "Me" ByRef in VB.NET?

I was shocked just a moment ago to discover that the following is legal (the C# equivalent is definitely not): Class Assigner ''// Ignore this for now. Public Field As Integer ''// This part is not so weird... take another instance…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
4
votes
3 answers

Is 'this' a local variable?

Here I am taking an example of overloading the increment operator: class Digit { int m_digit; public: Digit (int value) //constructor { m_digit = value; } Digit& operator++(); int…
user5241471
  • 105
  • 5
3
votes
2 answers

How does the Visual C++ compiler pass the this ptr to the called function?

I'm learning C++ using Eckel's "Thinking in C++". It states the following: If a class contains virtual methods, a virtual function table is created for that class etc. The workings of the function table are explained roughly. (I know a vtable is…
Hinton
  • 2,320
  • 4
  • 26
  • 32
3
votes
2 answers

constant function is called by non constant object. Why?

I am a newbie in c++ and facing a problem with constant objects. I have declared a constant member function named function (and as I have learned that a constant function can only be called by a constant object) but here a regular object calls a…
3
votes
4 answers

Is memcpy with this pointer safe?

I'm currently writing an own string implementation in C++. (Just for exercise). However, I currently have this copy-constructor: // "obj" has the same type of *this, it's just another string object string_base(const string_base &obj) :…
cocoz1
  • 119
  • 5
3
votes
3 answers

Function returning "This" object in C++

So, following is a member function of Class Sales_data which is defined outside the class, Sales_data& Sales_data::combine(const Sales_data &rhs) { units_sold += rhs.units_sold; revenue += rhs.revenue; //adding the members of rhs into the…
daljinder singh
  • 177
  • 1
  • 9
3
votes
2 answers

Is it necessary to use 'new' operator in the following C++ code?

The code is: class base{ base(){} virtual base* copy()const=0; virtual ~base(){} }; class derived:public base{ derived(){} base* copy()const; ~derived(){} }; base* derived::copy()const{ return new derived(*this); } Is it…
Tong
  • 83
  • 5
3
votes
2 answers

Use super class's address/pointer in initialization list

context 1: class D : public B1, public B2{}; context 2: B2 takes B1 to initialize: B2( B1 * ) //B2's constructor my question is in D's initialization list: D::D() : B1(), B2( ? )... What should be in ? I don't want to put " (B1*)this " in the ?…
JQ.
  • 678
  • 7
  • 17
3
votes
4 answers

Is There a Benefit or Performance Boost By Using 'this ->' to Reference Members of a Class?

I was wondering is there was an advantage of any kind by using 'this' to reference class members, rather than not using it, in c++? for example... class Test { public: Test(); void print_test() { …
anacy
  • 399
  • 4
  • 14
3
votes
1 answer

This-pointer capture in lambda wrapper around recursive function

I have a class template Wrap with a recursive member function test(int) that I want to pass to an STL algorithm with a lambda (std::accumulate in the code below). If I use a default capture list of =, and make my recursive meber function static,…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
3
votes
7 answers

Is using *this a good idea?

I'm not sure if return *this is the only way we could return an instance of a class who called a member function? The reason why I asked is because our instructor told us to avoid using pointers if necessary and I'm wondering if this is a case…
Charles Khunt
  • 2,375
  • 5
  • 25
  • 25
2
votes
2 answers

Is there no such thing as "implicit this parameter" in the Standard?

Recently, I asked this question where one of the answers says: There's no such thing as "implicit this parameter" in the standard. The standard calls it an "implicit object parameter". Then someone commented that: There's no such thing as…
Jason
  • 36,170
  • 5
  • 26
  • 60
2
votes
5 answers

c++ this pointer question

here is the thing, I want to (probably not the best thing to do) have the ability to call some class constructor that receives as a parameter a pointer to the class who's calling (ufff!!!). Well in code looks better, here it goes, as I do it in…
Yoss
  • 428
  • 6
  • 16
1 2
3
9 10