A const object cannot call other non-const member functions, and a const member function cannot call other non-const members.

In C language, we know that variables modified by the const keyword have constant attributes, referred to as constant variables. That is, the modified variable cannot be modified again, and C++ is compatible with the C language and also has this feature, but C++ introduces the reference (&), so there are more uses of const.

We know that the referenced object must be a variable and cannot have constant attributes, so objects modified with const cannot be referenced.

A const object cannot call non-const member functions
 

class Date
{
public:
	Date(int year=2023)
		:_y(1970)
	{
		_y = year;
	}
	void Print() 
	{
		cout << _y << "年" <<endl;
	}
private:
	int _y;

};
int main()
{
	const Date d1;
	d1.Print();
}

As above, d1 is modified by const. At this time, the compilation will not pass and an error will be reported:

Incompatibility means that d1 has been modified by const, has constant attributes, and cannot be called. +

Since the parameter is passed implicitly and automatically when calling a member function, the pointer of d1 is passed, and Date* this is accepted in the member function. At this time, the type is incompatible. In the final analysis, I am still afraid that you will change the value of the d1 object, because the address is implicitly passed (similar to a reference), and the pointer this is received, and this actually points to the space of the d1 object, so an error will be reported.

But if you pass d1, there will be no problem.


class Date
{
public:
	Date(int year=2023)
		:_y(1970)
	{
		_y = year;
	}
	void Print(Date d) 
	{
		cout << _y << "年" <<endl;
	}
private:
	int _y;

};
int main()
{
	const Date d1;
	Date d;
	d.Print(d1);
}

Solution

 So for type compatibility, the founder has his solution: (Add const modification at the end of the member function)

 

A const member function cannot call other non-const members

class Date
{
public:
	Date(int year=2023)
		:_y(1970)
	{
		_y = year;
	}
	void out()
	{
		cout << "haha" << endl;
	}
	void Print() const
	{
		cout << _y << "年" <<endl;
		out();
	}
private:
	int _y;

};
int main()
{
	const Date d1;
	d1.Print();
}

Take a look at this. I created an out member function and called it in the Print member function. The same error was reported:

 The error reason is the same: incompatibility. In the final analysis, it is still implicit parameter passing. When the out member function is called, the parameter is actually passed. What is passed is *this, which is the d1 object, and d1 is modified by const, so the type is incompatible. The same is true in the out member function. Add const at the end to modify it and it will compile.

class Date
{
public:
	Date(int year=2023)
		:_y(1970)
	{
		_y = year;
	}
	void out()const
	{
		cout << this << endl;
	}
	void Print() const
	{
		cout << this << endl;
		out();
	}
private:
	int _y;

};
int main()
{
	const Date d1;
	d1.Print();
}

 

There is another little knowledge point: const-modified objects must be initialized when they are created, and the same goes for references.

 

Guess you like

Origin blog.csdn.net/C_Rio/article/details/131995965