Cursor this in C ++

Each object in C ++ has access to its own location through an important pointer called this pointer. The pointer this in C ++ is a hidden parameter to all member functions. So inside a member function, this pointer can refer to the calling object.

The friend functions do not have this pointer, because friend is not a member of a class. Only member functions in C ++ have this this pointer.

Consider the following example to understand the concept of this pointer in C ++:

 #include using namespace std ; class Box { public : // phan dinh nghia Constructor Box ( double dai = 1.0 , double rong = 1.0 , double cao = 1.0 ) { cout << "Constructor duoc goi!" << endl ; chieudai = dai ; chieurong = rong ; chieucao = cao ; } double theTich () { return chieudai * chieurong * chieucao ; } int sosanh ( Box box ) { return this -> theTich () > box . theTich (); } private : double chieudai ; // chieu dai cua mot box double chieurong ; // chieu rong cua mot box double chieucao ; // chieu cao cua mot box }; int main ( void ) { Box Box1 ( 2.4 , 4.2 , 2.2 ); // khai bao box1 Box Box2 ( 4.5 , 2.0 , 3.2 ); // khai bao box2 if ( Box1 . sosanh ( Box2 )) { cout << "Box2 la nho hon Box1" << endl ; } else { cout << "Box2 la bang hoac lon hon Box1" << endl ; } return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Cursor this in C ++ Picture 1

According to Tutorialspoint

Previous article: Inline function in C ++

Next lesson: Cursor to class in C ++

3.5 ★ | 4 Vote

May be interested

  • How to change the mouse cursor in WindowsHow to change the mouse cursor in Windows
    how to change the mouse cursor in windows - in this article, i will show you how to change the mouse cursor in the following two main ways.
  • Replace the mouse cursor with funny picturesReplace the mouse cursor with funny pictures
    instead of the default windows mouse cursor, you can create more funny and lovely mouse pointers that suit your taste and help create more interest in working with computers.
  • How to change the cursor size on the Debian 10 desktopHow to change the cursor size on the Debian 10 desktop
    debian allows you to change the cursor size via the settings utility. you can access the settings utility through the application launcher search.
  • Macromedia Flash - Dynamic cursor objectMacromedia Flash - Dynamic cursor object
    read this exercise and see how to create a dynamic cursor with just a few basic steps and of course use action script in flash 8.
  • Macromedia Flash - Move objectsMacromedia Flash - Move objects
    with this exercise, you will learn how to move any object with the cursor in flash 8 using the action script.
  • The basic steps of fixing cursor cursors flutter in WindowsThe basic steps of fixing cursor cursors flutter in Windows
    the status of the mouse pointer jumps, the mouse is disordered or the mouse moves automatically while typing the keyboard occurs quite popular, it causes inhibition for the user to keep the mouse to hold the cursor back. this may be because the touchpad on your laptop is too sensitive or maybe because you accidentally touched the touchpad when typing. please try to apply the following corrections below.
  • Macromedia Flash - Push objects away from the cursorMacromedia Flash - Push objects away from the cursor
    this tutorial will show you how to make a flash file with the effect of pushing the object away from the cursor by using some action script fragments in flash 8. let's look at the following example file.
  • Create snow effect when moving mouse on WindowsCreate snow effect when moving mouse on Windows
    in many parts of the world, snow began to fall and turn everything into a white color from the roof, the street, the park, the playground, everything looked like a long white sheet of paper. you can create snowfall in your computer using the cursor snowflakes software, making it snow every time you move the mouse on the screen.
  • How to Restart Windows 8How to Restart Windows 8
    to restart windows 8, move the cursor to the upper/lower right corner → click → click the button → click . you can also use an alternate method that uses only the keyboard in situations where you cannot use the mouse. move your cursor to th...
  • Mouse cursor spins error in Windows 10+11 and how to fix it quicklyMouse cursor spins error in Windows 10+11 and how to fix it quickly
    are you feeling annoyed because the mouse pointer keeps spinning in windows 10+11? it's okay, tipsmake has measures in place