effective c++ 条款3 use const whereever you can
2014-06-25 03:32
387 查看
1 const 传达的意思应该是这个变量是常量不能更改
2 const 在 * 左边表示数据是const,在右边表示指针是const
3
4 const 成员函数
声明方式
作用
ConstMemberFunc.h
ConstMemberFunc.cpp
2 const 在 * 左边表示数据是const,在右边表示指针是const
// char greeting[] = "hello"; char* p = greeting; //const *: const data //* const: const pointer char const *p1 = greeting; // const data char * const p2 = greeting; // const pointer const char * const p3 = greeting; // const data and pointer //not correct p1 is const data //(*p1)++; //correct p1 is const data p1++; //correct p2 is const pointer (*p2)++; //not correct p2 is const pointer //p2++; //not correct p3 is const data and pointer //(*p3)++; //not correct p3 is const data and pointer //p3++;
3
//1 const return value avoid the error like this // if( a+b = c) {...} //2 const parameter // it can avoid unintentioned change of parameter inside the function const Rational operator+ (const Rational& lhs, const Rational& rhs) { Rational tmp; tmp.real = lhs.real + rhs.real; tmp.img = lhs.img + rhs.img; return tmp; } class Rational { public: float real; float img; Rational():real(0),img(0){}; };
4 const 成员函数
声明方式
作用
ConstMemberFunc.h
//--------------------------------------------------------------------------- #ifndef ConstMemberFuncH #define ConstMemberFuncH //--------------------------------------------------------------------------- #include <cstddef> //for std::size_t //1 const member function is used to operate const object // eg. we have a class Textblock and it has a operator[] which is const // when we have a const Textblock, the operator[] const will be used //2 // a const member function can change static class member // b const member function can not change non-static class member // c use mutable decleared variable can be changed in const member function //3 const member function can only call const member function // can not call non-const member function //4 const object can only call const member function // const object can not call non const member function class TextBlock { public: static int TextBlockStaticNum; char* text; //1 const member function is used to operate const object // eg. we have a class Textblock and it has a operator[] which is const // when we have a const Textblock, the operator[] const will be used const char& operator[](std::size_t position) const { return text[position]; //validate 3 //not correct //3 const member function can only call const function // can not call non-const function // print(); is the non-const function of vector }; char& operator[](std::size_t position) { //return text[position]; //call const operator[] and remove constness for return //not recommended return const_cast<char&>( // remove the const from the return of const [] static_cast<const TextBlock&>(*this) // add const to *this [position] // call const [] ); }; //2 // a const member function can change static class member // b const member function can not change non-static class member // c use mutable decleared variable can be changed in const member function int nonStaticOrMutable; mutable int textLen; const void constMemberFunctionTest(int Num) const { //a change static member TextBlockStaticNum = 1; //b //not correct //can not change non-static class member //nonStaticOrMutable = 1; //c use mutable decleared variable can be changed in const member function textLen = 2; }; //3 const member function can only call const member function // can not call non-const member function void print() { ; } TextBlock( char charArray[]) { text = charArray; TextBlockStaticNum = 0; textLen = 0; nonStaticOrMutable=0; }; } ; //1 const member function is used to operate const object // eg. we have a class Textblock and it has a operator[] which is const // when we have a const Textblock, the operator[] const will be used //Test function extern void ConstAndNonConstObjCallConstMemberFunction(); //2 // a const member function can change static class member // b const member function can not change non-static class member // c use mutable decleared variable can be changed in const member function extern void ConstMemFunctionChangeClassMember(); //4 const object can only call const member function // const object can not call non const member function extern void ConstObjCanNotCallNonConstMember(const TextBlock & ctb) { //not correct //print is not a const member function //ctb.print(); //correct const char t = ctb[0]; }; #endif
ConstMemberFunc.cpp
//--------------------------------------------------------------------------- #pragma hdrstop #include "ConstMemberFunc.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #include "ConstMemberFunc.h" void ConstAndNonConstObjCallConstMemberFunction() { TextBlock tb("test"); const TextBlock ctb("test"); //call non const operator[] //correct tb[0] = '1'; //call const operator[] //not correct, since ctb is const operator //ctb[0] = '1'; //normally this is used as a function call //e.g. void print(const TextBlock ctb) //we do not change ctb in this function } void ConstMemFunctionChangeClassMember() { TextBlock tb("test"); tb.constMemberFunctionTest(5); const TextBlock ctb("test"); ctb.constMemberFunctionTest(2); }
相关文章推荐
- Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at kernel/timeconst.pl line
- Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at kernel/timeconst.pl
- Python sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings......
- HTML在线编辑器29个( 29 Rich Editor Controls that you can use with ASP.NET)
- effective c++ 条款03 尽可能的使用const (const 的魅力)
- effective C++ 3 use const whenever possible
- 读书笔记《Effective c++》 条款02 尽量用const,enum,inline代替#define
- effective c++:条款21: 尽可能使用const
- effective c++ 条款20(用const引用代替值传递)
- [Effective C++]条款02 尽量以const,enum,inline替换#define
- Posts Tagged ‘This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register问题的解决办法
- 《effective c++》条款二:尽量以enum,const,inline替换#define
- Effective C++:条款02:尽量以const,enum,inline替换#define
- 60款开源云应用【Part 2】(60 Open Source Apps You Can Use in the Cloud)
- Python:You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byt
- effective C++ 条款 3:尽可能使用const
- effective C++ 条款 20:宁以pass-by-reference-to-const替换pass-by-value
- Effective C++学习笔记:条款1:尽量用const和inline而不用#define
- Effective C++阅读笔记_条款2:尽量以const,enum,inline替换#define
- Question[SQL]:Can you use a SQL statement to calculating it!