C++ this指针的理解和做用

01 C++ 程序到 C 程序的翻译

要想理解 C++ 的 this 指针,咱们先把下面的 C++ 代码转换成 C 的代码ios

class Car 
{
public:
	int m_price;         // 成员变量
	void SetPrice(int p) // 成员函数
	{
	    m_price = p; 
	}
};

int main()
{
	Car car;
	car.SetPrice(20000); // 给car对象m_price成员变量赋值
	
	return 0;
}
复制代码

C 语言是没有类定义的class关键词,可是有跟class相似的定义,那就是结构体structbash

m_price变量是Car类的成员变量,那么咱们能够把Car类和成员变量翻译成以下的 C 代码:函数

// 结构体Car
struct Car
{
    // price变量是属于Car结构体这个域里的变量
    int price;  
};
复制代码

SetPrice函数是Car类的成员函数,可是 C 程序里是没有成员函数这种概念的,因此只能把成员函数翻译成全局的函数:ui

// 参数1:结构体Car的指针
// 参数2:要设置的价格变量
void SetPrice(struct Car* this, int p)
{ 
    this->price = p;  // 将传入的Car结构体的price变量赋值
}
复制代码

为何要加个 this 的指针呢?咱们继续往下看。this

在这里咱们把上面main函数下面的 C++ 程序翻译 C 程序是这样的:spa

int main() 
{
    struct Car car;
    SetPrice( &car, 20000);
    return 0;
}
复制代码

因此最终把上述的 C++程序 转换成C 程序的代码以下:翻译

struct Car
{
    int price;  
};


void SetPrice(struct Car* this, int p)
{ 
    this->price = p; 
}

int main() 
{
    struct Car car;
    SetPrice( &car, 20000); // 给car结构体的price变量赋值
    return 0;
}
复制代码

02 this指针的做用

其做用就是指向成员函数所做用的对象, 因此非静态成员函数中能够直接使用 this 来表明指向该函数做用的对象的指针。指针

#include <iostream>

class Car 
{
public:
	int m_price;
	
	void PrintPrice()
	{
		std::cout << m_price << std::endl;	
	}
	
	void SetPrice(int p)
	{
		this->m_price = p; // 等价于 m_price = p;
		this->PrintPrice();// 等价于 PrintPrice();
	}
	
	Car GetCar()
	{
	    return *this; // 返回该函数做用的对象
	}
};

int main(void)
{
	Car car1, car2;
	car1.SetPrice(20000);
	
	// GetCar()成员函数返回所做用的car1对象,所把返回的car1赋值给了car2
	car2 = car1.GetCar(); 
	car2.PrintPrice();   
	
	return 0;
}
复制代码

输出结果:code

20000
20000
复制代码

接下来咱们下面的代码,你以为输出结果是什么呢?会出错吗?对象

class A
{
    int i;
    public:
    void Hello() { cout << "hello" << endl; }
};

int main()
{
    A * p = NULL;
    p->Hello(); //结果会怎样?
}
复制代码

答案是正常输出hello,你可能会好奇明明 p 指针是空的,不该该是会程序奔溃吗?别着急,咱们先把上面的代码转换C程序,就能理解为何能正常运行了。

void Hello() { cout << "hello" << endl; } 
# 成员函数至关于以下形式:
void Hello(A * this ) { cout << "hello" << endl; }

p->Hello(); 
# 执行Hello()形式至关于:
Hello(p);
复制代码

因此,实际上每一个成员函数的第一个参数默认都有个指向对象的 this 指针,上述状况下若是该指向的对象是空,至关于成员函数的第一个参数是NULL,那么只要成员函数没有使用到成员变量,也是能够正常执行。

下面这份代码执行时,就会奔溃了,由于this指针是空的,使用了 空的指针指向了成员变量i,程序就会奔溃。

class A
{
    int i;
public:
    void Hello() { cout << i << "hello" << endl; }
    // ->> void Hello(A * this ) { cout << this->i << "hello" << endl; }
};
int main()
{
    A * p = NULL;
    p->Hello(); //  ->> Hello(p); 
}
复制代码

03 this指针和静态成员函数

静态成员函数是不能使用 this 指针,由于静态成员函数至关因而共享的变量,不属于某个对象的变量。


04 小结

  • 经过将C++程序翻译成C程序的方式,来理解 this 指针,其做用就是指向非静态成员函数所做用的对象,每一个成员函数的第一个参数实际上都是有个默认 this 指针参数。

  • 静态成员函数是没法使用this指针,

相关文章
相关标签/搜索