本文共 4692 字,大约阅读时间需要 15 分钟。
讲讲const的基本用法 --------------------------------------------------------------- 1.声明常量 const int a=0; a=5; //error 2.修饰函数行参,保证行参在函数中不被修改 void fn(int i) { i=5;//error } 3.修饰成员函数,使成员函数只能调用成员变量,且使用时不被改变 class A { int i; int f()const{return i;} } --------------------------------------------------------------- const declaration member-function const When modifying a data declaration, the const keyword specifies that the object or variable is not modifiable. When following a member function's parameter list, the const keyword specifies that the function doesn't modify the object for which it is invoked. // Constant Values The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it. const int i = 5; i = 10; // Error i++; // Error In C++, you can use the const keyword instead of the #define preprocessor directive to define constant values. Values defined with const are subject to type checking, and can be used in place of constant expressions. In C++, you can specify the size of an array with a const variable as follows: const int maxarray = 255; char store_char[maxarray]; // Legal in C++; illegal in C In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files. The const keyword can also be used in pointer declarations. char *const aptr = mybuf; // Constant pointer *aptr = 'a'; // Legal aptr = yourbuf; // Error A pointer to a variable declared as const can be assigned only to a pointer that is also declared as const. const char *bptr = mybuf; // Pointer to constant data *bptr = 'a'; // Error bptr = yourbuf; // Legal You can use pointers to constant data as function parameters to prevent the function from modifying a parameter passed through a pointer. You can call constant member functions only for a constant object. This ensures that the object is never modified. birthday.getMonth(); // Okay birthday.setMonth( 4 ); // Error You can call either constant or nonconstant member functions for a nonconstant object. You can also overload a member function using the const keyword; this allows a different version of the function to be called for constant and nonconstant objects. You cannot declare constructors or destructors with the const keyword. // Constant Member Functions C++ Specific Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant. END C++ Specific Example // Example of a constant member function class Date { public: Date( int mn, int dy, int yr ); int getMonth() const; // A read-only function void setMonth( int mn ); // A write function; // cannot be const private: int month; }; int Date::getMonth() const { return month; // Doesn't modify anything } void Date::setMonth( int mn ) { month = mn; // Modifies data member } --------------------------------------------------------------- #define MAX 100 /* C 语言的宏常量*/ const int MAX = 100; // C++ 语言的const 常量 const float PI = 3.14159; // C++ 语言的const 常量 const 与#define 的比较 C++ 语言可以用const 来定义常量,也可以用#define 来定义常量。但是前者比后 (1) const 常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行类型安全检查。而对后者只进行字符替换,没有类型安全检查,并且在字符替换可能会产生意料不到的错误(边际效应)。 (2) 有些集成化的调试工具可以对const 常量进行调试,但是不能对宏常量进行调试 如果参数是指针,且仅作输入用,则应在类型前加const,以防止该 指针在函数体内被意外修改。 void StringCopy(char *strDestination,const char *strSource); 如果输入参数以值传递的方式传递对象,则宜改用“const &”方式 来传递,这样可以省去临时对象的构造和析构过程,从而提高效率。 // 用const 提高函数的健壮性 看到const 关键字,C++程序员首先想到的可能是const 常量。这可不是良好的条件反射。如果只知道用const 定义常量,那么相当于把火药仅用于制作鞭炮。const 更大的魅力是它可以修饰函数的参数、返回值,甚至函数的定义体。 const 是constant 的缩写,“恒定不变”的意思。被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。所以很多C++程序设计书籍建议:“Use const whenever you need” --------------------------------------------------------------- char const* pc 和cosnt char * pc是一样的. // const修饰一个指针时,将有五种组合: 1.const char *pc; 2.char const *pc; 3.char * const pc; 4.const char * const pc; 5.char const * const pc; 解读方法: 方法一:(这个方法最简单,建议使用) A.首先找到"*"号; B."*"号左边有"const",表示指针所指向的对象是const类型的. C."*"号右边有"const",表示指针本身是const类型的. D."*"号两边都有"const",表示不仅指针所指向的对象是const类型的,而且指针本身也是const类型的.俗话叫"两头都被绑死了"; 方法二: A.原则,从最右边往左边读: B.比如:char const * const pc;顺序如下:pc -> const -> * -> const -> char;我们这样读:"pc" 是一个"const"类型的指针("*"号的读法),它指向一个"const"类型的 "char"值. 这种方法没有"方法一"快,但读出来的意思要完整些,"方法一"用来快速判定"const"到底修饰的是指针本身还是指针所指向的对象.转载地址:http://fopmb.baihongyu.com/