Answers for "c++ is this a pointer"

C++
1

What is This pointer? Explain with an Example.

Every object in C++ has access to its own address through an important pointer called this pointer.
The this pointer is an implicit parameter to all member functions.
Therefore, inside a member function, this may be used to refer to the invoking object.

Example:
#include <iostream>
using namespace std;
class Demo {
private:
  int num;
  char ch;
public:
  void setMyValues(int num, char ch){
    this->num =num;
    this->ch=ch;
  }
  void displayMyValues(){
    cout<<num<<endl;
    cout<<ch;
  }
};
int main(){
  Demo obj;
  obj.setMyValues(100, 'A');
  obj.displayMyValues();
  return 0;
}
Posted by: Guest on November-30-2020
2

pointer c++

int myvar = 6;
int pointer = &myvar; // adress of myvar
int value = *pointer; // the value the pointer points to: 6
Posted by: Guest on September-12-2021
2

what is this pointer in c++

Every object in C++ has access to its own address through an important pointer called this pointer.
 The this pointer is an implicit parameter to all member functions. 
Therefore, inside a member function,
 this may be used to refer to the invoking object.

Friend functions do not have a this pointer,
 because friends are not members of a class. 
Only member functions have a this pointer.
Posted by: Guest on May-14-2021
0

pointer in c++

int* pointVar, var;
var = 5;

// assign address of var to pointVar
pointVar = &var;

// access value pointed by pointVar
cout << *pointVar << endl;   // Output: 5

In the above code, the address of var is assigned to the pointVar pointer.
We have used the *pointVar to get the value stored in that address.
Posted by: Guest on September-09-2020

Browse Popular Code Answers by Language