faqts : Computers : Programming : Languages : C++

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 24 people (50%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

C++: How to use the 'this' command in C++?

May 6th, 2008 09:37
Vishal Sharma, Knud van Eeden, Colin Thomsen,


----------------------------------------------------------------------
--- Knud van Eeden --- 28 April 2021 - 04:10 pm ----------------------
C++: How to use the 'this' command in C++?
---
Using the 'this' keyword
---
To tell the compiler which variable names correspond to class members
and which variable names correspond to method parameters, precede the 
class
member variable names with the 'this' keyword, as shown here:
---
// save as ConstructorOverload.cpp
#include <iostream.h>
#include <conio.h>
//
class CLASSEmployee {
 private:
  string nameS;
  int employeeidI;
  int officenumberI;
  double salaryD;
  //
  static int defaultidI;
  static int defaultofficeI;
  static double defaultsalaryD;
  //
 public:
  void PROCEmployeeShowRecord();
  void PROCEmployeeShowRecord( int xI, int yI );
  void PROCVariableSetDefault( int defaultidI, int defaultofficeI, 
double defaultsalaryD );
  CLASSEmployee( string nameS, int employeeidI, int officenumberI, 
double salaryD );
  CLASSEmployee( string nameS );
};
//
 int CLASSEmployee::defaultidI = 300;
 //
 int CLASSEmployee::defaultofficeI = 200;
 //
 double CLASSEmployee::defaultsalaryD = 10000.0;
 //
// function overload
 void CLASSEmployee::PROCEmployeeShowRecord() {
  cout << "Name = " << nameS << endl;
  cout << "Office = " << officenumberI << endl;
  cout << "Employee Id = " << employeeidI << endl;
  cout << "Salary = $" << salaryD << endl;
  cout << "----------------" << endl;
 }
// function overload
 void CLASSEmployee::PROCEmployeeShowRecord( int xI, int yI ) {
  gotoxy( xI, yI + 0 * 1 ); cout << "Name = " << nameS;
  gotoxy( xI, yI + 1 * 1 ); cout << "Office = " << officenumberI;
  gotoxy( xI, yI + 2 * 1 ); cout << "Employee Id = " << employeeidI;
  gotoxy( xI, yI + 3 * 1 ); cout << "Salary = $" << salaryD;
 }
 // constructor function overload
 CLASSEmployee::CLASSEmployee( string nameS, int employeeidI, int 
officenumberI, double salaryD ) {
  this->nameS = nameS;
  this->employeeidI = employeeidI;
  this->officenumberI = officenumberI;
  this->salaryD = salaryD;
 }
 // constructor function overload
 CLASSEmployee::CLASSEmployee( string nameS ) {
  this->nameS = nameS;
  employeeidI = defaultidI++;
  officenumberI = defaultofficeI++;
  salaryD = defaultsalaryD;
 }
//
void main() {
  CLASSEmployee bossO( "Suzanne" );
  CLASSEmployee topworkerO( "Vanessa", 101, 124, 25000.0 );
  CLASSEmployee worker1O( "Nathalie" );
  //
  bossO.PROCEmployeeShowRecord();
  topworkerO.PROCEmployeeShowRecord();
  worker1O.PROCEmployeeShowRecord();
}
//
/*
if you run this you will see e.g.:
c:\borland\bcc55\bin\bcc32.exe -Ic:\borland\bcc55\include -
Lc:\borland\bcc55\lib c:\bbc\taal\constr~1.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
c:\bbc\taal\ConstructorOverload.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
constr~1.exe
Name = Suzanne
Office = 200
Employee Id = 300
Salary = $10000
----------------
Name = Vanessa
Office = 124
Employee Id = 101
Salary = $25000
----------------
Name = Nathalie
Office = 201
Employee Id = 301
Salary = $10000
----------------
Press any key when ready...
*/
[book: see also: Schildt, Herbert - C++, the complete reference / 2th 
edition - McGraw-Hill - ISBN 0078821231]
---
---
Internet: see also:
---
C#: How to use the 'this' command in C#?
http://www.faqts.com/knowledge_base/view.phtml/aid/24776/fid/791
---
Delphi: How to use the 'this' command (='self') in Delphi?
http://www.faqts.com/knowledge_base/view.phtml/aid/16108/fid/175
---
Java: How to use the 'this' command in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/16104
---
Visual Basic: How to use the 'this' command (='Me') in Visual 
Basic.NET?
http://www.faqts.com/knowledge_base/view.phtml/aid/27372/fid/1610
----------------------------------------------------------------------
---
Comment by Colin Thomsen:
NOTE:
Most C++ programmers avoid the need to use 'this' to differentiate
between class member variables and class method parameters by adding a
prefix or suffix to the member variables.
---
For example:
class A
{
public:
    void set(int v1, int v2, int v3)
    {
        _v1 = v1;
        _v2 = v2;
        _v3 = v3;
    }
private:
    int _v1;
    int _v2;
    int _v3;
};
---
In the above example, I use the prefix '_'. Often this is used as a
suffix instead.
The benefits are:
1) Less typing - not adding this-> to things
2) Clearly see when a member variable is being used throughout the code
3) Less chance of accidentally missing the 'this'
4) More common in C++ community.
---
More typical reasons to use 'this' include:
1) In assignment operators, where you return a copy of the object after
the copy, *this is the right thing to return, eg:
class A
{
    A &operator=(const A &rhs)
    {
        // do the assignment to rhs
        ...
        return *this;
    {
};
2) To use operators inside another class method. eg [] operator inside
print function:
class A
{
    // maybe for a vector, return the element at pos
    int operator[](int pos) const
    {
        return _a_vector[pos];
    }
    void print()
    {
        for(int i=0; i < _a_vector.size(); i++)
             std::cout << (*this)[i];
    }
    ...
};
3) In some template code. Do not quite remember the details, but there
is a difference between calling a class method using this->f() and just
f()
http://christmas-gifts-idea.blogspot.com/
http://uniquegiftsideas.blogspot.com/
http://valentinesdaygiftsidea.blogspot.com/