C++: Getting to know classes and this pointer


insert image description here

Personal homepage: Personal homepage
Personal column: "Data Structure" "C Language" "C++"

foreword


one type

class definition and instantiation

insert image description here
Note that the semicolon ( ; ) at the end of the class definition cannot be omitted.

The contents of the class body are called members of the class:

  • Variables in a class are called attributes or member variables of the class;
  • A function in a class is called a method or member function of the class;

Two ways to define a class

  • The declaration and definition are all placed in the class body, please note: if the member function is defined in the class, the compiler may treat it as an inline function
//日期类
class Date
{
    
    
public:
	//打印日期
	void Print()
	{
    
    
		cout << _year << "/" << _month << "/" << _day << endl;
	}

private:
	int _year;  //年
	int _month; //月
	int _day;   //日
};

  • Class declarations are placed in .h files, and member functions are defined in .cpp files. It should be noted that defining functions outside the class should add the class name:: (scope of the class)

insert image description here
insert image description here


class instantiation

The process of creating an object of a class type is called instantiation of the class.

  • A class describes an object, defines which members the class has, and defines a class without allocating actual memory space.
  • A class can instantiate multiple objects, and the instantiated objects occupy the actual physical space and store class member variables
//日期类
class Date
{
    
    
public:
	//打印日期
	void Print()
	{
    
    
		cout << _year << "/" << _month << "/" << _day << endl;
	}

	void Init(int year = 0, int month = 0, int day = 0)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;  //年
	int _month; //月
	int _day;   //日
};


int main()
{
    
    
	// 类的实例化
	Date d;
	d.Init(2023, 9, 3);

	d.Print();
}

insert image description here


class access qualifier

In the definition of the above class, we saw that the two keywords private and public are the access qualifiers of the class.
insert image description here
The scope of the access qualifier: from the position where the access qualifier appears until the next access qualifier appears or if there is no access qualifier after that, the scope is to } that is, the end of the class.

  • Publicly modified members can be directly accessed outside the class
  • Protected and private modified members cannot be directly accessed outside the class
  • The default access permission of class is private, and the default access permission of struct is public

Private modified members cannot be directly accessed outside the class.
insert image description here
insert image description here

Members modified by public can be directly accessed outside the class

//日期类
class Date
{
    
    
public:
	//打印日期
	void Print()
	{
    
    
		cout << _year << "/" << _month << "/" << _day << endl;
	}

	void Init(int year = 0, int month = 0, int day = 0)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;  //年
	int _month; //月
	int _day;   //日
};


int main()
{
    
    
	Date d;
	d.Init(2023, 9, 3);

	d.Print();

	//d._year = 0;
}

insert image description here


class scope

A class defines a new scope, and all members of the class are in the scope of the class.
When defining a member outside a class, you need to use the :: scope operator to indicate which class domain the member belongs to.

//日期类
class Date
{
    
    
public:
	//打印日期
	void Print();

	void Init(int year = 0, int month = 0, int day = 0);
private:
	int _year;  //年
	int _month; //月
	int _day;   //日
};

void Date::Init(int year = 0, int month = 0, int day = 0)
{
    
    
	_year = year;
	_month = month;
	_day = day;
}

void Date::Print()
{
    
    
	cout << _year << "/" << _month << "/" << _day << endl;
}

Calculate the size of the class

Storage method of class objects

  • Only member variables are saved, and member functions are stored in the public code area
//日期类
class Date
{
    
    
public:
	//打印日期
	void Print()
	{
    
    
		cout << _year << "/" << _month << "/" << _day << endl;
	}

	void Init(int year = 0, int month = 0, int day = 0)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;  //年
	int _month; //月
	int _day;   //日
};

int main()
{
    
    
	Date d1;
	Date d2;

	return 0;
}

For the following code objects d1, d2 are displayed as follows:

insert image description here


So what is the size of the Date class?
insert image description here
Conclusion: The size of a class is actually the sum of the member variables in the class, and the same attention should be paid to memory alignment as in calculating the size of a structure.
Note that the size of the empty class is 1. The compiler gives an empty class a byte to identify objects of this class

Structure memory alignment


Second, the this pointer of the member function of the class

//日期类
class Date
{
    
    
public:
	//打印日期
	void Print()
	{
    
    
		cout << _year << "/" << _month << "/" << _day << endl;
	}

	void Init(int year = 0, int month = 0, int day = 0)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;  //年
	int _month; //月
	int _day;   //日
};

int main()
{
    
    
	Date d1;
	d1.Init(2023, 9, 3);
	d1.Print();

	Date d2;
	d2.Init(2023, 9, 3);
	d2.Print();
}

For the above code, there is such a problem, we use the Date class to instantiate two objects d1, d2. So when d1 calls the Init function, how does the function know whether it should set the d1 object, or the d2 object?

C++ solves this problem through the this pointer. The C++ compiler adds a hidden pointer parameter to each "non-static member function", so that the pointer points to the current object (the object that calls the function when the member function is running), in the function body The operation of all member variables in is accessed through this pointer. It's just that all the operations are transparent to the user, and the compiler completes them automatically.


Characteristics of this pointer

  • The type of this pointer: class type * const, that is, in member functions, the value of this cannot be changed.
    insert image description here
    insert image description here

  • Can only be used inside member functions
    insert image description here

  • The this pointer is essentially a formal parameter of a member function. When an object calls a member function, the object address is passed as an actual parameter to this formal parameter. So the this pointer is not stored in the object (the size of the object is only the sum of member variables)

  • The first implicit pointer parameter of this pointer member function is usually passed automatically by the compiler through the ecx register and does not need to be passed by the user.

  • cannot be written explicitly in the formal parameters of member functions
    insert image description here


Here is a question.
What is the result for the following code?

class A
{
    
    
public:
	void Print()
	{
    
    
		cout << "void Print()" << endl;
	}

private:
	int _a;
};

int main()
{
    
    
	A* p = nullptr;
	p->Print();

	return 0;
}

insert image description here
The result works fine. Why?
Because Print is a member function and is not stored inside the object, but is stored in the public code area. The compiler does not access the location of nullptr, but directly goes to the public code area to find the Print function.
insert image description here


Summarize

The above is my summary of the first class and this pointer in C++. Thanks for the support! ! !
insert image description here

Guess you like

Origin blog.csdn.net/li209779/article/details/132548376