Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende The This Keyword | Fundamentals of OOP in C++
C++ OOP
course content

Contenido del Curso

C++ OOP

C++ OOP

1. Fundamentals of OOP in C++
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
The This Keyword

The this keyword in object-oriented programming refers to the current object instance. It provides member functions with a pointer to the object that invoked them, allowing access to that object's variables and functions.

cpp

Example

copy
12345678910111213
#include <iostream> class Example { public: Example* get_address() { return this; } }; int main() { Example obj; std::cout << &obj << std::endl; std::cout << obj.get_address() << std::endl; }

When you use the dot operator (.) to access member variables or member functions within a class, the this pointer implicitly points to the object for which the member function is called. This is often valuable in various scenarios and commonly employed to avoid ambiguity in variable names.

cpp

Example

copy
123456789101112131415
#include <iostream> class Example { public: void multiply(int number) { number *= number; } int number; }; int main() { Example obj; obj.number = 2; obj.multiply(5); std::cout << obj.number; }

Here, we can utilize the this keyword since it points to the current object as a pointer. To access its attributes, we must use the -> operator.

cpp

main

copy
123456789101112131415
#include <iostream> class Example { public: void multiply(int number) { this->number *= number; } int number; }; int main() { Example obj; obj.number = 2; obj.multiply(5); std::cout << obj.number; }

Using the this keyword is a widespread practice across various scenarios. It's often employed, particularly in large classes, to signify that a variable is a member of the classes and not an external parameter or argument.

question mark

What does the this keyword refer to object-oriented programming?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 7

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

course content

Contenido del Curso

C++ OOP

C++ OOP

1. Fundamentals of OOP in C++
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
The This Keyword

The this keyword in object-oriented programming refers to the current object instance. It provides member functions with a pointer to the object that invoked them, allowing access to that object's variables and functions.

cpp

Example

copy
12345678910111213
#include <iostream> class Example { public: Example* get_address() { return this; } }; int main() { Example obj; std::cout << &obj << std::endl; std::cout << obj.get_address() << std::endl; }

When you use the dot operator (.) to access member variables or member functions within a class, the this pointer implicitly points to the object for which the member function is called. This is often valuable in various scenarios and commonly employed to avoid ambiguity in variable names.

cpp

Example

copy
123456789101112131415
#include <iostream> class Example { public: void multiply(int number) { number *= number; } int number; }; int main() { Example obj; obj.number = 2; obj.multiply(5); std::cout << obj.number; }

Here, we can utilize the this keyword since it points to the current object as a pointer. To access its attributes, we must use the -> operator.

cpp

main

copy
123456789101112131415
#include <iostream> class Example { public: void multiply(int number) { this->number *= number; } int number; }; int main() { Example obj; obj.number = 2; obj.multiply(5); std::cout << obj.number; }

Using the this keyword is a widespread practice across various scenarios. It's often employed, particularly in large classes, to signify that a variable is a member of the classes and not an external parameter or argument.

question mark

What does the this keyword refer to object-oriented programming?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 7
Lamentamos que algo salió mal. ¿Qué pasó?
some-alt