Learn a little trick to improve yourself C++ every day. Clause 03: Use const as much as possible

/*
*
*This blog post and this series are my own thoughts on watching "Effective C++", and I learn a little trick to improve my C++ every day.
*
*

Item 03: Use const whenever possible

  • const allows you to specify a semantic constraint (specify an object that should not be modified) (that is, define a constant), and the compiler will enforce this constraint, the compiler can help you tell your customers that the data should not be changed .

  • Const can be used to modify all variables or constants of the namespace scope (namespace) outside the class, or to modify the objects declared as static in the file, function, or block scope (local).

  • It can also be used to modify the static and non-static member variables inside the class.

  • In the face of a pointer, you can point out that the pointer itself, what the pointer refers to, or both cannot be modified.

char str[] = "Hello";
char* p = str;             //都可以修改
const char* p = str;       //数据不可以修改,指针可以修改
char* const p = str;       //指针不可以修改,数据可以修改
const char* const p = str; //指针和数据都不可以修改

skill:

1. The pointer that const appears to the left of the asterisk can be modified, but the data cannot be modified.
2. The pointer that const appears on the right of the asterisk cannot be modified, but the data can be modified.
3. Both const representations cannot be modified.

The following two wordings represent the same meaning and are used by people

void func1(const int* a)
{
    
    
	//...
}

void func2(int const* a)  // func1、func2都是获取同一类型的参数
{
    
    
	//...
}

Use const for STL iterator should be written like this

int main()
{
    
    
	vector<int> v;
	const vector<int>::iterator it1 = v.begin(); //指针是常量,所指数据不是。
	*it1 = 2;
	//it1++;        //出错,it1是const

	vector<int>::const_iterator it2 = v.begin(); //数据是常量,指针不是。
	*it2++;
	//*it2 = 3;    //出错, *it2是const
}

Make the function return value const

class Rational {
    
    
	//..
};
const Rational operator*(const Rational& lhr, const Rational& rhs)
{
    
    
	//..
}

//如果不这样做客户可能出现下面的做法,给两个数值的乘积赋值.
//也许客户的本意是if(a*b == c)少打一个=造成的错误,加上const就会省下恼人的错误
int main()
{
    
    
	Rational a, b, c;
	//(a * b) = c;  //函数返回值const的话这句话就报错提醒
}

const member function

Reasons for adding const to member functions
1. It makes the interface of the class easier to understand. You can know which function can change the content of the object.
2. It makes it possible to "manipulate const objects".

The two member functions are just different in constants and can be overloaded

class TextBook
{
    
    
public:
	TextBook(string str)
	{
    
    
		this->text = str;
	}
	const char& operator[](size_t position)const   //const版本
	{
    
    
		return text[position];
	}
	char& operator[](size_t position)				//非const版本
	{
    
    
		return text[position];
	}
private:
	string text;

};

int main()
{
    
    
	TextBook tb("Hello");						//非const版本
	const TextBook ctb("World");				//const版本
	cout << tb[0] << endl;  //读合法
	tb[0] = 'X';			//写合法
	
	cout << ctb[0] << endl;  //读合法
	//ctb[0] = 'x';			//写不合法
}

A keyword mutable as opposed to const

class CTextBook
{
    
    
public:
	size_t length()const;
private:
	char* pText;
	mutable size_t textLength;   
	mutable bool lengthIsValid;  //这些成员是可变的即使在const成员函数内
};
size_t CTextBook::length()const  //一般来说在const成员函数内不能给成员赋值
{
    
    
	if (!lengthIsValid)
	{
    
    
		textLength = strlen(pText);
		lengthIsValid = true;  //因为给textLength和lengthIsValid加上mutable才能合法赋值
	}
	return textLength;
}

Guess you like

Origin blog.csdn.net/weixin_50188452/article/details/111084225