C++指针、this指针、静态成员

試著忘記壹切 提交于 2019-11-26 13:01:21

C++指针、this指针、静态成员

一、C++ this指针

this指针指向调用的对象本身,在成员函数中,都会有一个默认的参数this。这点如果学过Python的话,可能会知道,在Python中每一个成员函数都会一个参数self,这里的this就是差不多的东西。其实具体来说,this指针也是指针,它存储的也是一个地址。当我们用一个对象去调用一个成员函数时,我们会把该对象的地址传给该函数,这也就是在函数中的this参数。因此this指针中存储的是调用对象的地址。
下面来看一个简单的例子:

#include <iostream>
using namespace std;

class Line
{
	public:
		int getL();
		void setL(int l);
		void printL();
		Line();
		~Line();
	private:
		int len;
};

Line::Line()
{
	cout << "创建对象" << endl;
}

Line::~Line()
{
	cout << "释放内存" << endl;
}

void Line::setL(int l = 3)
{
	this->len = l;
}

int Line::getL()
{
	return this->len;
}

void Line::printL()
{
	int temp;
	temp = this->getL();
	cout << "Len is:" << temp << endl;
}



int main()
{
   Line line,line2;
	line.setL(10);
	line2.setL();
	line.printL();
	line2.printL();
   return 0;
}

在编程的过程中会发现许多问题,在用指针来得到结构体或者类的成员时我们不能使用’.'运算符来,需要使用->来访问才不会报错。其次,对于一个成员函数如何要使用参数的默认值,在类中定义时,不需要给出默认的函数参数。不然也会报错。
运行结果:

创建对象
创建对象
Len is:10
Len is:3
释放内存
释放内存

刚才看到使用printL()函数利用this指针来调用成员函数和成员变量。还有另外的一种方式来实现同样的操作,那就是友元函数。友元函数不属于类,但可以访问类的所有成员,包括私有成员和保护成员。与成员函数不同的是它没有this指针,它不属于类。

#include <iostream>
using namespace std;

class Line
{
	public:
		friend void friend_test(Line line);
		int getL();
		void setL(int l);
		void printL();		
		Line();
		~Line();
	private:
		int len;
};

Line::Line()
{
	cout << "创建对象" << endl;
}

Line::~Line()
{
	cout << "释放内存" << endl;
}

void Line::setL(int l = 3)
{
	this->len = l;
}

int Line::getL()
{
	return this->len;
}

void Line::printL()
{
	int temp;
	temp = this->getL();
	cout << "Len is:" << temp << endl;
}

void friend_test(Line line)
{
	cout << "Len is :"<< line.len << endl;
}

int main()
{
   Line line,line2;
	line.setL(10);
	line2.setL();
	line.printL();
	line2.printL();
	friend_test(line);
	friend_test(line2);
   return 0;
}

运行结果:

创建对象
创建对象
Len is:10
Len is:3
Len is :10
释放内存
Len is :3
释放内存
释放内存
释放内存

可以看出,在友元函数中可以直接访问私有变量。

二、类的指针

类的指针同普通变量的指针一样存储对象在内存中的地址。就像上面的this指针一样。
看一个小例子:

#include <iostream>
using namespace std;

class Line
{
	public:
		int getL();
		void setL(int l);
		Line();
		~Line();
	private:
		int len;
};

Line::Line()
{
	cout << "创建对象" << endl;
}

Line::~Line()
{
	cout << "释放内存" << endl;
}

void Line::setL(int l = 3)
{
	this->len = l;
}

int Line::getL()
{
	return this->len;
}



int main()
{
    Line line,line2;
	Line *p,*q;
	p = &line;
	q = &line2;
	p->setL(10);
	q->setL();
	cout << "line Len is: " << p->getL() << endl;
	cout << "line2 Len is:" << q->getL() << endl;
	
   return 0;
}

运行结果:

创建对象
创建对象
line Len is: 10
line2 Len is:3
释放内存
释放内存

需要注意的是,类的指针访问类的成员时需要使用->这个运算符。

三、类的静态成员

类中的静态的成员,意思是该类的所有对象共用这一个变量,不会随着对象的创建而被拷贝。讲到通俗一点就是,用static修饰的变量的地址已经锁定了,不可以更改。
静态成员的用法,当静态成员被定义时需要进行初始化。如果没有初始化,那么当该类创建第一个对象时,会自动为所有的静态成员变量进行初始化,值为0;并且在类中定义的静态变量不可以在类中定义时就对其初始化。可以使用:类型 类名::变量名 = 0这种形式来进行初始化。

#include <iostream>
using namespace std;

class Line
{
	public:
		static int count;
		int getL();
		void setL(int l);
		Line();
		~Line();
	private:
		int len;
};

int Line::count = 0;

Line::Line()
{
	cout << "创建对象" << endl;
	count++;
}

Line::~Line()
{
	cout << "释放内存" << endl;
	count--;
}

void Line::setL(int l = 3)
{
	this->len = l;
}

int Line::getL()
{
	return this->len;
}



int main()
{
    Line line;
	cout << "已将创建的对象有:"<< line.count << " 个" <<endl;
	Line lines[3];
	cout << "已将创建的对象有:"<< line.count << " 个" <<endl;	
   	return 0;
}

运行结果:

创建对象
已将创建的对象有:1 个
创建对象
创建对象
创建对象
已将创建的对象有:4 个
释放内存
释放内存
释放内存
释放内存
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!