在c++编程中,this
是一个引用类的当前实例的关键字。 this
关键字在c++中可以有3
个主要用途。
下面来看看看this
关键字在c++中的例子,它指的是当前类的字段。
#include <iostream>
using namespace std;
class employee {
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
float salary;
employee(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
employee e1 =employee(101, "hema", 890000); //creating an object of employee
employee e2=employee(102, "calf", 59000); //creating an object of employee
e1.display();
e2.display();
return 0;
}
输出结果如下 -
101 hema 890000
102 calf 59000