Question

In: Computer Science

What does this and *this mean in c++ And how to use them

What does this and *this mean in c++
And how to use them

Solutions

Expert Solution

This represents a POINTER in C++

Every object in C++ have access to its own address through an important pointer called as this pointer

in C++ this pointer is used to hold the address of an object inside a member function

for example;

let say there is an object called obj which is calling its own member function method() as obj.method() then this pointer will hold the address of object obj inside the member function method()

ultimately we can say that it will point to the object for which this function is called.

it is automatically passed to the member function when it is called i.e when you call obj.method() this will set to address of obj

for eample:

the following progrm:

#include<iostream.h>
#include<conio.h>

class UseThisPointer{
int b;
public: void setData(int b){
this->b = b; //this will separate the member data with argument
   }
   void printData()
   {
   cout<<"The value of b is"<<b<<endl; }
   };
int main(){
clrscr();
   UseThisPointer tp;
   tp.setData(10);
   tp.printData();
   getch();
   return 0;
   }

will produce the output:

the value of b is 10

b is a private data member of class UseThisPointer since argument of method setData is also b without using this explicitly both will be taken as data members

Use of this in C++

Return Object

one of the most important use of this pointer in C++ is to return object this points to

for example

the statement

*this

inside a member function will return the object which calls the fuction


Related Solutions

what does this "->" mean in c? when can you use it?
what does this "->" mean in c? when can you use it?
How does thought influence the use of language? What does this mean for you as a...
How does thought influence the use of language? What does this mean for you as a professional communicator in a business setting?
What does break and continue mean in C++?
What does break and continue mean in C++?
what is a const in C++? what does const mean in these functions
what is a const in C++?what does const mean in these functions-> " void idkwhattonameit( const name) {} "-> " void idkwhattonameit2( const &name) {} "-> " void idkwhattonameit3( const * name) {} "-> " void idkagain (const unique_ptr < Name > name_uPtr)"
What does the term performance mean? Expected performance? b. What does the term risk mean? c....
What does the term performance mean? Expected performance? b. What does the term risk mean? c. Can the risk be completely eliminated? Why? d. In the financial securities market, how can the level of risk be minimized? and. What does variability mean within corporate finance? F. What does the concept of diversification mean within corporate finance? What is its application within a global stock market?
What are the ledgers, why do we use them? And then HOW do we use them,...
What are the ledgers, why do we use them? And then HOW do we use them, how does information get into them how do balances get extracted. And then what should the balances for various accounts be, i.e. assets, liabilities, expenses, revenues, equity, dividends. Why SHOULD they have a particular balance as either debit or credit.
What costs does Uber or Airbnb have and how does it manage them? What is the...
What costs does Uber or Airbnb have and how does it manage them? What is the key to Uber’s success?
For encryption, what does it mean for data to be in transition and in use? What...
For encryption, what does it mean for data to be in transition and in use? What are the major security concerns with data encryption?
What are neurons? How to use them to construct AND, OR, and NOT gates?
What are neurons? How to use them to construct AND, OR, and NOT gates?
explain how does racial discrimination in the workplace by them the employee them salves and what...
explain how does racial discrimination in the workplace by them the employee them salves and what we can do. ( 250) words explain how age discrimination happens in the workplace by the employee them salves. (250) words
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT