this pointer supplement, const supplement, static supplement, static const int

Keywords: Python C C++

catalogue

Object calling function and this pointer (supplemented by this pointer)

const supplement

const decorated data member (initializing member list)

Modify member functions (data members will not be modified in member functions)

Decorated object

static supplement

Modified out of class

Modify global variables

Modify local variables

Modifier class

Decorate data members

Decorated member function

static const int

Object calling function and this pointer (supplemented by this pointer)

Each object data has its own data, storage space and function code will be shared. This can save space and pass each object data through this pointer

const supplement

const decorated data member (initializing member list)

#include <iostream>

using namespace std;

#if 0
class A
{
public:
	A(int a)
	{
		//m_a = a; 	// error 	 Non modifiable lvalue
	}
	void aShow()
	{
		cout << m_a << endl;
	}

private:
	const int m_a;
};
#endif

#if 0
class A
{
public:
	A(int a = 11)	//error
	{

	}
	void aShow()
	{
		cout << m_a << endl;
	}

private:
	const int m_a;
};

int main()
{
	A a(11);
	a.aShow();

	return 0;
}
#endif

#if 0
class A
{
public:
	A(int a) :m_a(a)	//correct 	 This is the initialization member list (real initialization), which is before the object is created
	{

	}
	void aShow()
	{
		cout << m_a << endl;
	}

private:
	const int m_a;
};

int main()
{
	A a(11);
	a.aShow();

	return 0;
}
#endif

#if 1
class A
{
public:
	A(int a = 4543)	//correct
	{
		//a = 1111;
	}

	A()
	{

	}

	void aShow()
	{
		cout << m_a << endl;
	}

private:
	const int m_a = 11;
};

int main()
{
	A a(1232);
	a.aShow();	//No matter how it is spread, he is 11

	return 0;
}
#endif

Modify member functions (data members will not be modified in member functions)

Const defines the object itself. Data members cannot be modified. Only member functions modified by const can be called

const qualifies member functions and can constitute overloads

#include <iostream>

using namespace std;

#if 0
class A
{
public:
	A(int num, int num2, int num3) :m_num(num), m_num2(num2), m_num3(num3)
	{

	}

	void aShow()
	{
		m_num = 11;
		m_num2 = 22;
		m_num3 = 33;
		cout << m_num << " " << m_num2 << " " << m_num3 << endl;
	}
private:
	int m_num;
	int m_num2;
	int m_num3;
};
#endif

#if 0
class A
{
public:
	A(int num, int num2, int num3) :m_num(num), m_num2(num2), m_num3(num3)
	{

	}

	void aShow()const	//error const restricts that the data members of the object itself cannot be modified
	{
		m_num = 11;
		m_num2 = 22;
		m_num3 = 33;
		cout << m_num << " " << m_num2 << " " << m_num3 << endl;
	}
private:
	int m_num;
	int m_num2;
	int m_num3;
};
#endif

class A
{
public:
	A(int num, int num2, int num3) :m_num(num), m_num2(num2), m_num3(num3)
	{

	}

	void aShow()const	//error const restricts that the data members of the object itself cannot be modified
	{
		cout << m_num << " " << m_num2 << " " << m_num3 << endl;
		aChange();	//Only member functions decorated with const can be called
	}

	void aChange()
	{
		m_num = 11;
		m_num2 = 22;
		m_num3 = 33;
		cout << m_num << " " << m_num2 << " " << m_num3 << endl;
	}

	void aChange() const	//const can constitute an overload
	{
		//m_num = 11;
		//m_num2 = 22;
		//m_num3 = 33;
		cout << m_num << " " << m_num2 << " " << m_num3 << endl;
	}
private:
	int m_num;
	int m_num2;
	int m_num3;
};

int main()
{
	A a(1, 2, 3);
	a.aShow();	//11 22 33 11 22 33

	return 0;
}

Decorated object

Const modified objects can only call member functions modified by const

For non const modified objects, the non const modified member function will be called first. If not, the const modified member function will be called

#include <iostream>

using namespace std;

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

	void aShow() const
	{
		cout << "void aShow() const" << endl;
	}

	void aNotConst()
	{
		cout << "aNotConst()" << endl;
	}

	void aConst() const
	{
		cout << "void aConst() const" << endl;
	}
};

int main()
{
	const A a;
	a.aShow();	//void aShow() const

	//a.aNotConst(); 	// error can only call member functions decorated with const

	A aa;
	aa.aShow();	//void aShow() preferentially calls non const decorated member functions

	aa.aConst();	//Void acost() const can call the member function modified by const

	return 0;
}

static supplement

Modified out of class

Modify global variables

         Qualified scope (can only be used in the current file)

Modify local variables

         Extended life cycle (during process operation)

Modifier class

Decorate data members

         Use to share data with ethnic groups (that is, objects under a class or subclasses under this class can be shared)

         In class definition and out of class initialization (can only be initialized out of class, static cannot be added during initialization)

         The storage space does not belong to the class itself

Decorated member function

         The member function modified by static does not have this pointer. It belongs to a class (in fact, it can be understood that it does not belong to an object)

         Only static member data can be called in static member functions, and ordinary member data cannot be called (because there is no this pointer)

#include <iostream>

using namespace std;

class A
{
public:
	void aShow()
	{
		cout << ms_num << endl;
	}

	void aModify(int num)
	{
		this->ms_num = num;
	}

	int& aReturnModify()
	{
		cout << m_num << endl;
		return ms_num;
	}

	static int& aReturnStaticModify()
	{
		//cout << m_ num << endl; 	// error static member function can only call static member data, not ordinary member data
		return ms_num;
	}
private:
	static int ms_num;
	int m_num;
};

//It can only be defined outside the class. That's the rule. There's nothing to tangle with
int A::ms_num = 11;

int main()
{
	cout << sizeof(A) << endl;	//4 static data members do not occupy class space

	A a, aa, aaa;
	a.aShow();		//11
	aa.aShow();		//11
	aaa.aShow();	//11

	aa.aModify(22);
	a.aShow();		//22
	aa.aShow();		//22
	aaa.aShow();	//22

	aaa.aReturnModify() = 33;	//Random value
	a.aShow();		//33
	aa.aShow();		//33
	aaa.aShow();	//33

	aaa.aReturnStaticModify() = 33;
	a.aShow();		//33
	aa.aShow();		//33
	aaa.aShow();	//33

	return 0;
}

static const int

#include <iostream>

using namespace std;

class A
{
public:
	const static int& aGetNum()
	{
		return MS_NUM;
	}
	void aShow()
	{
		cout << MS_NUM << endl;
	}
private:
	//static const int MS_NUM = 11;
	static const int MS_NUM;
};

const int A::MS_NUM = 11;

int main()
{
	A a;
	cout << a.aGetNum() << endl;
	a.aShow();

	return 0;
}

Posted by tomwerner on Mon, 27 Sep 2021 04:07:18 -0700