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
0
votes
2 answers

Passing a node as an outstream operator

This prints an error message about qualifiers but don't really understand what that means and how to adjust the code for it to work? Anyways, thanks a lot for looking at the code. Note: The ostream operator is friended in the Node class. using…
Jason A.
  • 71
  • 1
  • 6
0
votes
0 answers

how does c++ compiler handle "this" pointer in virtual function

i got a confuse about how does c++ compiler handle "this" pointer in virtual function, my code: #include #include #include #include using namespace std; class A { public: void…
梁立浩
  • 1
  • 1
0
votes
1 answer

Returning this pointer when the return type of a function is a reference (C++)

There is a very similar question here but I do not understand the following: Imagine that I have a class for 2D mathematical vectors and I want to define the setter functions: /** Setters **/ Vector2D& setX(double xcoor) {x = xcoor; return…
Angelos
  • 169
  • 10
0
votes
2 answers

Using this pointer to write object to binary file in c++

void Employee::store_data(string filename) { fstream file; file.open(filename,ios::app | ios::binary); if (file) { file.write((char*)&this,sizeof(this)); file.close(); } else cout<<"\n Error in Opening the…
0
votes
1 answer

When running a validation through the bot framework how can I assure properties are mutable that belong to the class the validator is in?

I have come across this issues a few times now and I'd like to know if there is something I can do to mitigate the issue. When using a waterfall dialog or dialog for that matter there are validators you can add to the dialog. These are associated…
Christian Matthew
  • 4,014
  • 4
  • 33
  • 43
0
votes
1 answer

Use of this pointer as rvalue

I have code which appears to work even though I have a bad feeling about it. My class Node has private members: // Node properties std::string m_keySymbol = ""; float m_nodeWeight = 0.0f; std::string m_displaySymbol = ""; // Ultimately will be…
user34299
  • 377
  • 2
  • 11
0
votes
2 answers

Clear function in a linked list node class C++

I am having trouble with making sure that I have created a clear function for a linked list Node class. I am using delete this which I know can cause memory issues, but this is the only way that I can think of to assure that all of the objects in…
0
votes
1 answer

Member functions called on non-pointer objects with std::function

Code is as the following. std::string::empty() should take the this pointer as the parameter whose type is pointer, std::string *. How could the calling at line 2 and 3 be OK? #include #include int main() { std::string…
chaosink
  • 1,329
  • 13
  • 27
0
votes
2 answers

Is the object "this" points to the same as a const object?

This question has to to do with overloading the assignment operator in c++. Take a look at the following code. It shows the function definition given by my book to overload the assignment operator. const cAssignmentOprOverload&…
0
votes
3 answers

'this' in C++ is a pointer to a reference?

I know this is silly and the title probably isn't the answer.. I always thought of this as a pointer to the current object which is supplied in every method call from an object (which is not a static method) but looking at what my code actually…
crommy
  • 433
  • 1
  • 4
  • 12
0
votes
1 answer

Initialize a class object with the this pointer in C++

In C++, I want to initialize (or change) a class object using the result of another class' method. Can I use the this pointer? Is there a better way? Dummy example: class c_A { public: int a, b; void import(void); }; class c_B { …
Medical physicist
  • 2,510
  • 4
  • 34
  • 51
0
votes
0 answers

Accessing array values within class, c++

I am implementing a set in C++ using an array where elements with values of 0 are not in the set and those with 1 are in the set. For example, if my set is {1,5} my array would look like: [0, 1, 0, 0, 0, 1]. I am overloading the "/" operator to…
0
votes
2 answers

C++ internals: Messing with the this-pointer

I have some questions about the internal workings of C++. I know for example that every member function of a class has an implied hidden parameter, which is the this-pointer (much in the same way Python does): class Foo { Foo(const Foo&…
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
0
votes
4 answers

Jquery closest tr from this fails, why?

Jquery is not my strong suit. This seems logically correct though it is not working. I have searched everything from find, to closest, to this, etc. I cannot figure out why this is not working. WHat I am trying to do is add a class to the closest tr…
Sabyre
  • 97
  • 10
0
votes
2 answers

How can I use this pointer with pointer to member function

I have typedef for a function pointer: typedef bool(WlanApiWrapper::* (connect_func) )(WLAN_AVAILABLE_NETWORK, const char *, const char *); and have a method that returns a pointer to function: const auto connect_method =…
Andriy
  • 362
  • 3
  • 16