[C ++] C ++オブジェクトモデルとこのポインタ

1.メンバー変数とメンバー関数の個別のストレージ

C ++では、クラス内のメンバー変数とメンバー関数は別々に格納されます

非静的メンバー変数のみがクラスのオブジェクトに属します

(1)空のオブジェクトのサイズ

#include <iostream>
using namespace std;

class Person
{
};

void test01() {
	Person p1;
	cout << "空对象的大小是:" << sizeof(p1) << endl;
}

int main(void)
{
	test01();

	system("pause");
	return 0;
}

空のオブジェクトが占めるメモリスペースは次のとおりです。1; C ++コンパイラは、空のオブジェクトのメモリ位置を区別するために、空のオブジェクトごとにバイトスペースも割り当てます。

すべての空のオブジェクトには、一意の一意のメモリアドレスも必要です。

#include <iostream>
using namespace std;

class Person
{
public:
	int m_A;     //非静态成员变量  属于类的对象上

	static int m_B;   //静态成员变量 不属于类的对象上

	void func(){}     //非静态成员函数  不属于类的对象上

	static void func2(){}  //静态成员函数  不属于类的对象上
};
int Person::m_B = 10;

void test01() {
	Person p1;
	cout << "p1的大小是:" << sizeof(p1) << endl;
}

int main(void)
{
	test01();

	system("pause");
	return 0;
}

2.このポインタの概念

メンバー変数とメンバー関数はC ++で別々に格納されることがわかっています

各非静的メンバー関数は関数インスタンスのみを生成します。つまり、同じタイプの複数のオブジェクトがコードの一部を共有します。

次に、問題は、このコードがどのオブジェクトがそれ自体を呼び出すかをどのように区別するかということです。

 

C ++は、特別なオブジェクトポインター、このポインターを提供することにより、上記の問題を解決します。thisポインターは、メンバー関数が呼び出されるオブジェクトを指します

このポインターは、すべての非静的メンバー関数を意味するポインターです。

このポインタは定義する必要はなく、直接使用できます

このポインタの目的:

  • 仮パラメーターとメンバー変数の名前が同じ場合、このポインターを使用して区別できます

  • クラスの非静的メンバー関数でオブジェクト自体を返すには、return * thisを使用できます。

(1)仮パラメーターとメンバー変数の名前が同じ場合、このポインターを使用して区別できます

これは使用しないでください

#include <iostream>
using namespace std;

class Person2
{
public:
	Person2(int age)
	{
		age = age;     //编译器会认为这三个age是一样的
	}

	int age;
};

void test02()
{
	Person2 p(18);
	cout << p.age << endl;
}

int main(void)
{
	test02();

	system("pause");
	return 0;
}

これを使って

#include <iostream>
using namespace std;

class Person2
{
public:
	Person2(int age)
	{
		this->age = age;    
	}

	int age;
};

void test02()
{
	Person2 p(18);
	cout << p.age << endl;
}

int main(void)
{
	test02();

	system("pause");
	return 0;
}

(2)クラスの非静的メンバー関数でオブジェクト自体を返すには、return * thisを使用できます。

ここで、常にp1の年齢をp2に追加する必要があります。

#include <iostream>
using namespace std;

class Person2
{
public:
	Person2(int age)
	{
		this->age = age;    
	}

	Person2& addAge(Person2& p)
	{
		this->age += p.age;
		return *this;
	}


	int age;
};

void test02()
{
	Person2 p1(10);

	Person2 p2(10);

	p2.addAge(p1).addAge(p1).addAge(p1);


	cout << p2.age << endl;
}

int main(void)
{
	test02();

	system("pause");
	return 0;
}

3.メンバー関数へのヌルポインターアクセス

C ++のnullポインターもメンバー関数を呼び出すことができますが、このポインターが使用されているかどうかにも注意してください。

このポインタを使用する場合は、コードの堅牢性を確保するために判断する必要があります

#include <iostream>
using namespace std;

class Person3
{
public:
	void showClass()
	{
		cout << "this is Person3" << endl;
	}

	void showAge()
	{
		cout << "age = " << age << endl;
	}

	int age;
};


void test03()
{
	//空指针访问成员函数
	Person3* p = NULL;
	p->showClass();    //空指针,可以调用成员函数

	p->showAge();      //但是如果成员函数中用到了this指针,就不可以了
}


int main(void)
{
	test03();

	system("pause");
	return 0;
}

次のようにコードを変更します。

#include <iostream>
using namespace std;

class Person3
{
public:
	void showClass()
	{
		cout << "this is Person3" << endl;
	}

	void showAge()
	{
		if (this == NULL)
		{
			return;
		}
		cout << "age = " << age << endl;
	}

	int age;
};


void test03()
{
	//空指针访问成员函数
	Person3* p = NULL;
	p->showClass();    //空指针,可以调用成员函数

	p->showAge();      //但是如果成员函数中用到了this指针,就不可以了
}


int main(void)
{
	test03();

	system("pause");
	return 0;
}

テスト後、静的メンバー関数にはnullポインターを介してアクセスすることもできます。

4.const変更されたメンバー関数

(1)定数関数

定数関数:

  • メンバー関数がconstに追加された後、この関数を定数関数と呼びます

  • 定数関数でメンバー属性を変更することはできません

  • メンバー属性宣言に変更可能なキーワードを追加した後でも、通常の関数で変更できます。

#include <iostream>
using namespace std;

class Person4
{
public:


	//1、this指针的本质是一个指针常量,指针的指向不可修改
	//5、如果想让指针指向的值也不可以修改,需要声明常函数
	void show()const    //5
	{
		//3、const Type* const pointer;
		//2、this = NULL; //不能修改指针的指向 Person* const this;
		//4、this->m_A = 100; //但是this指针指向的对象的数据是可以修改的

		//6、const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
		this->m_B = 100;
	}
	int m_A;
	mutable int m_B; //可修改 可变的
};


int main(void)
{
	

	system("pause");
	return 0;
}
  • このポインタの本質はポインタ定数であり、ポインタのポイントは変更できません
  • this = NULL; // Person *へのポインタを変更できませんconstthis;
  • this-> m_A = 100; //ただし、このポインタが指すオブジェクトのデータは変更できます
  • ポインタが指す値を変更できないようにする場合は、定数関数を宣言する必要があります。
  • void show()const {}
  • const変更されたメンバー関数。ポインターが指すメモリー空間内のデータは、可変によって変更された変数を除いて、変更できないことを示します。

(2)通常のオブジェクト

通常のオブジェクト:

  • オブジェクトを定数オブジェクトと呼ぶようにオブジェクトを宣言する前にconstを追加します

  • 定数オブジェクトは定数関数のみを呼び出すことができます

#include <iostream>
using namespace std;

class Person4
{
public:


	//1、this指针的本质是一个指针常量,指针的指向不可修改
	//5、如果想让指针指向的值也不可以修改,需要声明常函数
	void show()const    //5
	{
		//3、const Type* const pointer;
		//2、this = NULL; //不能修改指针的指向 Person* const this;
		//4、this->m_A = 100; //但是this指针指向的对象的数据是可以修改的

		//6、const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
		this->m_B = 100;
	}

	void MyFunc1() 
	{
		
	}

	void MyFunc2()const
	{

	}

	int m_A;
	mutable int m_B; //可修改 可变的
};


void test004()
{
	const Person4 person; //常量对象  
	cout << person.m_A << endl;
	//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问
	person.m_B = 100; //但是常对象可以修改mutable修饰成员变量


	//常对象只能调用常函数
	//person.MyFunc1();   //常对象不可用调用普通成员函数,因为普通成员函数可以修改属性
	
	person.MyFunc2();
}


int main(void)
{
	test004();

	system("pause");
	return 0;
}

 

おすすめ

転載: blog.csdn.net/Zhouzi_heng/article/details/113702802