pointer in cpp details

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.

0
0
Bippy 8155 points

                                    #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;
}

0
0
3.44
9
Awgiedawgie 440215 points

                                    #include <iostream>
using std::cout;

int main() {
  /* 
  Some things to keep in mind:
  	-you shouldn't circumvent the type system if you are creating raw ptrs
  	and don't need to "type pun" or cast (don't use void ptrs)
    -ptr types only reference memory (which are integers), not actual data, thus
    they should not be treated as data types
    char* is just 1 byte of mem, int* is just 4 bytes of mem, etc
    - '*' means that you are creating a pointer which "points" to the mem address
    of a variable
    - '&', in this case, means "get the mem address of this variable"
  */
  
  void* ptr; // a pointer that doesn't reference a certain size of memory
  int* int_ptr; // a pointer that points to data with
  				// only 4 bytes of memory (on stack)
  
  int a = 5; // allocates 4 bytes of mem and stores "5" there (as a primitive)
  ptr = &a; // can only access the memory address of 'a' (not the data there)
  
  int b = 45; 
  int_ptr = &b; // can access both memory address and data of 'b'
  
  cout << ptr << "\n"; // prints mem address of 'a'
  /*cout << *ptr << "\n"; <- this will error out; a void ptr cannot be 
  							 derefrenced */
  cout << *(int*)ptr << "\n"; // type punning to get around void ptr (extra work)
  
  cout << int_ptr << "\n"; // mem address of b
  cout << *int_ptr << "\n"; // data stored at b
  
  /* -- OUTPUTS -- */
  /*
  	some memory address (arbitrary) which contains 05 00 00 00 as its data
  	5
    some memory address (arbitrary) which contains 2D 00 00 00 as its data
    45
  */
  
  return 0; // you only need this if "main" isnt the linker entry point
  			// you also don't care
  
  // ur also probably wondering why I didn't using namespace std... cherno
}

3.44 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
use of 'this' pointer in class in c++ use of 'this' pointer in classe in c++ should i use pointers in c++ c++ pointer function c++ what does a pointer contain function pointers in cpp how often are pointers actually used in c++ c++ pointer in function pa how do pointers work in cpp pointer and function in cpp why would you use pointers in c++ pointer and function in c++ how to declare pointer in c+++ value of a pointer in c++ calling a pointer function in c++ pointer and new in cpp is c++ pointers does c++ language have pointers get pointer of variable c++ what's the point of a pointer in c++ what is the point of using pointers in c++ cpp pointer applications pointer operations in c++ How this pointer can be used in C++. Explain with example program. c++ pointer reference c++ why use pointers what is the point of pointers inc c++ pointers and types of pointers in cpp why use pointers n C++ reference pointer in c++ this pointers c++ does c++ have pointers c++ pointers in functions c++ in pointer declare pointers in class in c++ pointer in cpp details pointer function c++ why should i use pointers in c++ this pointer c++ explained pointer of structure in c++ what must we include to use pointers cpp Type of this pointer in C++ Pointer and functions with example. in c++ cpp function pointer type cpp function pointer define reference and pointer in function c++ c++ pointer declaration syntax function pointer to c++ pointer to function in cpp * pointers cpp different types of pointers in CPP pointers and functions c++ reference a pointer c++ object pointer in c++ c++ pointer function in class c++ pointer class function pointer in c++ operators reference and pointer in c++ pointer to pointer in cpp what is class pointer in c++ why to use pointers in c++ "pointer function" c++ using !! with pointer c++ using with pointer c++ why use pointer to pointer in c++ declare a function pointer c++ why are pointers useful in c++ c++ have pointers pointer declaration in c++ why we use pointers in c++ how to declare and use pointers c++ operations on pointers c++ pointer in class c++ c++ value of pointer c++ where does the * pointers write a program to implement pointers in c++ when use pointers in c++ c++ pointer to pointer class function of pointer c++ this pointer c++ types of pointers in cpp c++ reference pointer creating pointer to a class in c++ what is pointer in cpp pointers in c+ reference of a pointer c++ pointer definition in cpp using pointers c++ examples pointers to pointers in cpp c++ how to have cirkular pointers class pointer in c++ c++ pointer to pointer to pointer what is the use of & in pointers in c++ pointers and addresses in cpp c++ what is this pointer c++ how to work with pointer in function pointer object in c++ c++ how to declare pointer to function how to use pointers in classes in c++ cpp make a pointer to a pointer function pointer syntax c++ function pointer example in cpp c++ declare pointer how to implement pointers in c++ new cpp pointer syntax c++ function pointer pointer function in c++ how to create pointer object in c++ object and pointer in c++ states of a pointer in c++ how to declare a pointer c++ pointer reference in c++ access pointer value c++ What are pointers in C++. pointer variable in c++ with example use of new in declaring pointers c++ c++ pointers explained function pointer cpp pointers inc++ usage of pointers in c++ this pointer use in c++ how do pointers work in c++ how to create a pointer to a class in c++ should you use pointers in c++ pointers and addresses cpp what's the point of pointers c++ cpp pointer to pointer new pointer of class type in cpp pointer in c++ definition using pointers in functions c++ using pointers through functions c++ c++ pointers examples can i create a class pointer in cpp pointer to function in c++ "function" pointer c++ function" pointer c++ using this-> pointer in c++ pointer value of c++ class pointer c++ why use pointers in c++ c++ create pointer to function this pointer in c++ example simple pointer program using c++ value of pointer c++ why use pointers to pointers c++ cpp * on pointer pointers constructor in c++ what are pointers in c+++ pointer declaration example in c++ pointers explained c++ function pointer c++ why we use pointers in cpp use of function pointer in c++ what are pointers for c++ does c++ have pointers how to use pointer methodin class in cpp how to use pointer function in class i cpp this pointer in c++ intro to pointers c++ uses of this pointer in c++ declare pointer as pointer in cpp c++ when to use pointers how to use pointers in c++ this pointer in class c++ & c++ meaning pointers & c++= meaning pointers pointer class in c++ &pointer in cpp pointer example in c++ pointer pointer variable in c++ create a pointer cpp c++ why do we use pointers c++ pointer declaration pointer in c++ example function pointers c++ cpp function pointer c++ set pointer position c++ set cursor position c++ class pointer What is the “this” pointer in C++? Explain. pointer example in cpp what are pointers used for in c++ c++ are references pointers what is c++ pointers c++ why are pointers useful what is this pointer in c++ c++ purpose of pointers C++ function pointers c pointer++ why using pointers c++ pointer cpp function class function pointer cpp pointer to a function cpp C++ allows you to have pointers to pointers to pointers pointer to function c++ operations with a pointer c++ pointers math program cpp cpp pointers example programs cpp pointers example programa function pointer in c++ pointer to pointer in c++ access pointer to pointer in c++ define pointer in c++ with example is this a pointer in c++ when to use pointers c++ using a pointer c++ this pointer in cpp how to make a value a pointer cpp what is pointer in c++ with example c++ pointers POINTER IN CPP is pointers different c++ c++ using pointers c++ pointers ptrnukl c++ pointer to pointer pointer to pointer c++ using a pointer of class in cpp what is pointer to pointer c++ when to user pointers c++ function pointer in cpp pointers c++ & are there pointers to functions in c++ this pointer in c++ pointers cpp cpp pointers declare pointer in c++ get pointer cpp how to use int pointers in c++ pointers in c__ cpp pointer all about pointers in c++ how to create pointer variable c++ why would you need to use pointers in C++ how to create pointers in c++ create a pointer to a variable in c++ create a pointer variable in c++ ++ pointers what are c++ pointers used for declare pointer c++ cpp function pointer += c++ pointer example how to declare a pointer in c++ * in pointers C++ how to increment pointer address c++ make a variable with the name of the pointer its stored at C++ (ptr+i) c++ c++ get pointer of variable put address in pointer c++ how to create a variable with a name of its pointer C++ pointer type variables in c++ set value at pointer c++ A pointer variable in c++ c++ get the value from a pointer imncrement memory address c++ how to make a pointer in cpp c++ get pointer from value pointer declaration c++ pointer types iin c++ create a pointer to an element in c++ c++ pointer after c++ create variable with same memory how to get a pointer c++ how to find the pointer in c++ & pointers get address of variable c++ how to declare a pointer variable c++ how to get the content of a pointer in c++ understanding pointers in c++ c++ get pointer location of variable how to get the int for a pointer to the int C++ c++ & * location get set c++ pointers c++ pointer get value create int pointer pointer c++ definition c++ use elemtns and save pointer int *a c++ access value of a pointer c++ examples of pointers in c++ what are pointers in c++ definition of a pointer c++ what are pointers in cpp What are C++ pointers how to declare a variable as a pointer c++ set pointer c++ c++ pint kom pointers c++ com pointers c++ & in c++ int pointer ++ c++ int pointer c++ how to define a pointer c++ pointer symbols in c++ what defines the value of a pointer c++ pointer cpp pointers c++ explained how to get the value from address of pointer in c++ how to get a variable from a pointer in c++ c++ pointers tutorial c++ set variable with pointer c++ what is a pointer variable c++ variable positions what does it mean how to access a point in c++ how to insert value at a pointer in c++ pointer c++ program c++ get pointer address &*pointer c++ c++ assign value to pointer memory what do your use potiner for in C++ ptr C++ pointers and memory c++ what is a pointer cpp c++ pointer contains -858993460 what can cpp pointers be used for pointer variable c++ c++ pointer variable how do I print all pointers in c++ what is pointer in c++ .* in c++ assign float variable to pointer c++ pointers c++ definition how to get the value of pointer in c++ how to give an address a value with a pointer c++ get value of pointer cpp pointers in c++ explained c++ * how to make in address in c++ store a value c++ set pointer value c++ pointer poiner in c++ c++ what are pointers add variable at certain memory adress c++ location of a pointer C++ c++ what pointer has address cpp pointer c++ syntax how to create a pointer in c++ what is pointers c++ what do we mean by changing pointer type c++ declaring a pointer in c++ pointer in c++ what is a pointer in c++ pointers in c++ pointer c++ read variable from pointer c++ pointers c++ & c++ address to an address c++ pointers in cpp
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source