C++ this 指针
C++ this 指针
在 C++ 中,每一个对象都能通过 this
指针来访问自己的地址。this
指针是所有成员函数的隐含参数。在成员函数内部,它可以用来指向调用对象。
友元函数没有 this
指针,因为友元不是类的成员。只有成员函数才有 this
指针。this
关键字只能用于成员函数,不能用于被 static
修饰的静态函数。this
指代的是当前上下文。
this
指针是隐含在对象成员函数内的一种指针,成员函数通过 this
指针可以知道操作的是哪个对象的数据。静态成员函数内部没有 this
指针,静态成员函数不能操作非静态成员变量,静态成员函数是属于类,函数内部没有 this
指针。当形参和成员变量同名时,可用 this
指针来区分。
this
是 C++ 的一个关键字,也是一个 const
指针,它指向当前对象,通过它可以访问当前对象的所有成员,它的值是不能被修改的。当前对象,是指正在使用的对象。info.set_count(10);
中 info
是当前对象,this
指向 info
。this
是一个指针,要用 ->
来访问成员变量或成员函数。在对象被创建以后会给 this
赋值,赋值的过程是编译器自动完成的,不需要用户干预,用户也不能显式地给 this
赋值。this
指向当前对象,而且对于不同的对象,this
的值是不一样。
this
只能在成员函数内部使用,用在其他地方没有意义,也是非法的。当对象被创建后 this
才有意义,因此不能在 static
成员函数中使用。
this
实际上是成员函数的一个形参,在调用成员函数时将对象的地址作为实参传递给 this
。不过 this
这个形参是隐式的,它并不出现在代码中,而是在编译阶段由编译器默默地将它添加到参数列表中。
this
作为隐式形参,本质上是成员函数的局部变量,所以只能用在成员函数的内部,并且只有在通过对象调用成员函数时才给 this 赋值。
成员函数最终被编译成与对象无关的普通函数,除了成员变量,会丢失所有信息,所以编译时要在成员函数中添加一个额外的参数,把当前对象的首地址传入,以此来关联成员函数和成员变量。这个额外的参数,实际上就是 this,它是成员函数和成员变量关联的桥梁。
this
指代的是当前上下文,this
表示的对象是这个方法活动的上下文。当我们调用成员函数时,实际上是替某个对象调用它。成员函数通过一个名为 this
的额外隐式参数来访问调用它的那个对象,当我们调用一个成员函数时,用请求该函数的对象地址初始化 this
。
成员函数通过一个名为 this
的额外隐式参数来访问调用它的那个对象,当我们调用一个成员函数时,用请求该函数的对象地址初始化 this
。
1. Example
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
class Qiang {
public:
static int cnt;
int count;
void set_count(const int num) {
// count = num;
this->count = num;
}
};
// static
int Qiang::cnt = 9;
int main() {
Qiang info;
Qiang data;
std::cout << Qiang::cnt << std::endl;
std::cout << info.cnt << std::endl;
std::cout << data.cnt << std::endl;
info.cnt = 16;
std::cout << Qiang::cnt << std::endl;
std::cout << info.cnt << std::endl;
std::cout << data.cnt << std::endl;
info.set_count(10);
std::cout << "info.count = " << info.count << std::endl;
data.set_count(20);
std::cout << "data.count = " << data.count << std::endl;
std::cout << "Hello, World!\n" << std::endl;
return 0;
}
/home/yongqiang/CLionProjects/hash_table/cmake-build-debug/hash_table
9
9
9
16
16
16
info.count = 10
data.count = 20
Hello, World!
Process finished with exit code 0
2. Example
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
class Qiang {
public:
void set_name(const char *name);
void set_age(const int age);
void set_score(const float score);
void display();
private:
const char *name;
int age;
float score;
};
void Qiang::set_name(const char *name) {
this->name = name;
}
void Qiang::set_age(const int age) {
this->age = age;
}
void Qiang::set_score(const float score) {
this->score = score;
}
void Qiang::display() {
std::cout << this->name << " " << this->age << " " << this->score << std::endl;
}
int main() {
Qiang info;
std::string name = "yongqiang";
info.set_name(name.c_str());
info.set_age(16);
info.set_score(96.5);
info.display();
std::cout << "Hello, World!\n" << std::endl;
return 0;
}
/home/yongqiang/CLionProjects/hash_table/cmake-build-debug/hash_table
yongqiang 16 96.5
Hello, World!
Process finished with exit code 0
还没有评论,来说两句吧...