answersLogoWhite

0


Best Answer

The this pointer is a hidden argument that is automatically generated by the compiler to refer to the current instance of an object.

Consider the following snippet:

class foo{};

void bar(foo* f){

std::cout<

}

int main()

{

foo f;

bar(&f);

return(0);

}

The function, bar, knows which instance of foo it is working on because we must explicitly pass a pointer to that instance (the address of f). However, if bar were a member of foo, we do not need to pass a pointer to the instance because the compiler does it automatically for us:

class foo {

void bar(){std::cout<

};

int main()

{

foo f;

f.bar();

return(0);

}

Although the second example is what we would physically write, it's really just sugar-coating. Behind the Scenes, it's as if bar() were a non-member function and is actually implemented as per the first example, where we passed the address of the instance into the function. This is because there is only ever one instance of the bar function (regardless of how many instances of foo we create), and the compiler needs some way of determining which instance of foo the bar function should work with -- thus it generates a this pointer for us and hides the actual implementation behind sugar-coated code.

The hidden this pointer is also used implicitly (behind the scenes) whenever you call member methods from within other member methods of the same class. However, you can invoke methods explicitly by dereferencing the this pointer if you want to. For instance, to invoke the bar() method from within another method of foo, you'd use the following call:

(*this).bar();

More typically, you'd use the points-to operator instead:

this->bar();

But since the this pointer is implicit, you don't need to explicitly dereference members like this unless it helps to clarify your code (such as when a member method operates upon other instances of the same class, and specifying this helps to clarify exactly which members you are invoking upon which instances).

However, the this pointer really comes into its own when you actually need to refer to the current instance, such as when performing self-reference checks and returning references to the current instance. A classic example is the assignment operator which typically requires both:

class foo {

public:

foo& operator=(const foo& rhs) {

if( this != &rhs ) {

// perform assignment

}

return(*this);

}

};

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is meant by this pointer in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is Dazzling Pointer in c plus plus?

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer


What is 'this' pointer in c plus plus?

Address of the current object.


What is meant by an pointer in c program?

Pointer is like variable address the members in memory shell


What is null object in c plus plus?

a pointer that is not pointing to anything


What is an address in C plus plus programming?

An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.


What does multiplying a pointer by 5 in C plus plus mean How does it change the pointer's value?

Multiplication is yet another thing, what you should never do with pointers.


Which function is used to determine the position of the put pointer in a file in c plus plus?

The function ftell returns the position of the file pointer for a file.


What is meant by println in c plus plus?

println is not a C++ keyword.


Do you have pointer concept in c plus plus language?

Yes. All string variables are pointers as are other arrays.


What is the concept of asterisk in c plus plus?

An asterisk in C++, such as int *data, is what's known as a pointer. A pointer is like a regular variable, but instead of holding a value, a pointer holds the memory location of the value. It's a somewhat difficult concept, and you can learn more about it here: See related links section below...


Call by reference using pointer in c plus plus?

Example: void foo( MyClass&amp; object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer


Can you control ports through c plus plus?

Yes. If the ports are memory mapped, then you simply need a pointer to that address, and you need to declare the pointer as volatile. If they are I/O mapped, then you need to create an _asm{} block.