2、指针使用CONST
(1)指针本身是常量不可变
(char*) const pContent;
const (char*) pContent;
(2)指针所指向的内容是常量不可变
const (char) *pContent;
(char) const *pContent;
(3)两者都不可变
const char* const pContent;
(4)还有其中区别方法,沿着*号划一条线: 如果const位于*的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量; 如果const位于*的右侧,const就是修饰指针本身,即指针本身是常量。
3、函数中使用CONST
(1)const修饰函数参数
a.传递过来的参数在函数内不可以改变(无意义,因为Var本身就是形参)
void function(const int Var);
b.参数指针所指内容为常量不可变
void function(const char* Var);
c.参数指针本身为常量不可变(也无意义,因为char* Var也是形参)
void function(char* const Var);
d.参数为引用,为了增加效率同时防止修改。修饰引用参数时:
void function(const Class& Var); //引用参数在函数内不可以改变
void function(const TYPE& Var); //引用参数在函数内为常量不可变
这样的一个const引用传递和最普通的函数按值传递的效果是一模一样的,他禁止对引用的对象的一切修改,唯一不同的是按值传递会先建立一个类对象的副本, 然后传递过去,而它直接传递地址,所以这种传递比按值传递更有效.另外只有引用的const传递可以传递一个临时对象,因为临时对象都是const属性, 且是不可见的,他短时间存在一个局部域中,所以不能使用指针,只有引用的const传递能够捕捉到这个家伙.
(2)const 修饰函数返回值
const修饰函数返回值其实用的并不是很多,它的含义和const修饰普通变量以及指针的含义基本相同。
a.const int fun1() //这个其实无意义,因为参数返回本身就是赋值。
b. const int * fun2() //调用时 const int *pValue = fun2();
//我们可以把fun2()看作成一个变量,即指针内容不可变。 c.int* const fun3() //调用时 int * const pValue = fun2();
//我们可以把fun2()看作成一个变量,即指针本身不可变。 一般情况下,函数的返回值为某个对象时,如果将其声明为const时,多用于操作符的重载。通常,不建议用const修饰函数的返回值类型为某个对象或对某个对象引用的情况。原因如下:如果返回值为某个对象为const(const A test = A 实例)或某个对象的引用为const(const A& test = A实例) ,则返回值具有const属性,则返回实例只能访问类A中的公有(保护)数据成员和const成员函数,并且不允许对其进行赋值操作,这在一般情况下很少用到。
4、类相关CONST
(1)const修饰成员变量
const修饰类的成员函数,表示成员常量,不能被修改,同时它只能在初始化列表中赋值。 class A
{
…
const int nValue; //成员常量不能被修改
…
A(int x): nValue(x) { } ; //只能在初始化列表中赋值
}
(2)const修饰成员函数
const修饰类的成员函数,则该成员函数不能修改类中任何非const成员函数。一般写在函数的最后来修饰。
class A
{
…
void function()const; //常成员函数, 它不改变对象的成员变
量.
//也不能调用类中任何非const成员函数。
}
对于const类对象/指针/引用,只能调用类的const成员函数,因此,const修饰成员函数的最重要作用就是限制对于const对象的使用。
a. const成员函数不被允许修改它所在对象的任何一个数据成员。
b. const成员函数能够访问对象的const成员,而其他成员函数不可以。
(3)const修饰类对象/对象指针/对象引用 ? const修饰类对象表示该对象为常量对象,其中的任何成员都不能被修改。对于对象指针和对象引用也是一样。
? const修饰的对象,该对象的任何非const成员函数都不能被调用,因为任何非const成员函数会有修改成员变量的企图。
例如:
class AAA
{
void func1();
void func2() const;
}
const AAA aObj;
aObj.func1(); ×
aObj.func2(); 正确
const AAA* aObj = new AAA();
aObj-> func1(); ×
aObj-> func2(); 正确
三、将Const类型转化为非Const类型的方法
采用const_cast 进行转换。
用法:const_cast <type_id> (expression)
该运算符用来修改类型的const或volatile属性。除了const 或volatile修饰之外, type_id和expression的类型是一样的。
? 常量指针被转化成非常量指针,并且仍然指向原来的对象;
? 常量引用被转换成非常量引用,并且仍然指向原来的对象;
? 常量对象被转换成非常量对象。
四、使用const的一些建议 ? 要大胆的使用const,这将给你带来无尽的益处,但前提是你必须搞清楚原委; ? 要避免最一般的赋值操作错误,如将const变量赋值,具体可见思考题; ? 在参数中使用const应该使用引用或指针,而不是一般的对象实例,原因同上; ? const在成员函数中的三种用法(参数、返回值、函数)要很好的使用; ? 不要轻易的将函数的返回值类型定为const;
? 除了重载操作符外一般不要将返回值类型定为对某个对象的const引用; ? 任何不会修改数据成员的函数都应该声明为const 类型。
五、补充重要说明
? 类内部的常量限制:使用这种类内部的初始化语法的时候,常量必须是被一个常量表达式 初始化的整型或枚举类型,而且必须是static和const形式。
? 如何初始化类内部的常量:一种方法就是static 和 const 并用,在外部初始化,例如:
class A { public: A() {} private: static const int i; file://注意必须是静态的! }; const int A::i=3;另一个很常见的方法就是初始化列表: class A { public: A(int i=0):test(i) {} private: const int i; }; 还有一种方式就是在外部初始化, ? 如果在非const成员函数中,this指针只是一个类类型的;如果在const成员函数中,
this指针是一个const类类型的;如果在volatile成员函数中,this指针就是一个 volatile类类型的。
? new返回的指针必须是const类型的。
第二篇:Dan Saks总结的const所有用法
Dan Saks
const T vs.T constInmy last column, I discussed one
of the reasons why the rules by which
a compiler can place data into ROM
are a bit more complicated in C++
than they are in C.1I have more to
say about that subject, but before I
do, I’d like to reply to the following
query I received through e-mail from
Phil Baurer at Komatsu Mining
Systems:
“We’re having an interesting prob-
lem using const with a typedef. I
hoped you could comment on this sit-
uation. I am wondering if we are
bumping into some unknown (by us)
rule of the C language.
“We are using the Hitachi C com-
piler for the Hitachi SH-2 32-bit RISC
microcontroller. We thought the fol-
lowing code:
typedef void *VP;
const VP vectorTable[]
= {..<data>..}; (1)
should be identical to:
const void *vectorTable[]
= {..<data>..}; (2)
“However, the linker places
vectorTablein (1) into the CONSTANT
section, but it places vectorTablein
(2) into the DATAsection.
“Is this the proper behavior or a
bug in the compiler?”
This is proper behavior; it is not a
bug. You are indeed bumping into
some rules of the C language that
you apparently don’t know about.
Don’t feel bad; you’re not alone. Ibelieve many other C and C++ pro-Thus, *x[N]is a declarator indicatinggrammers are confused about thesethat xis an “array of Nelements ofrules, which is why I’m answering thispointer to ...” something, where thatin my column.something is the type specified in theI presented some of these rules indeclaration specifiers. For example,an earlier column.2However, in look-ing back at that column, I don’t thinkstatic unsigned long int *x[N];I emphasized strongly enough thepoints which seem to be the sourcedeclares xas an object of type “array ofof your confusion. So let me tryNelements of pointer to unsignedagain. long int.” (As explained later, the key-Although C and C++ read mostly from top-to-bottom and left-to-right, pointer declarationsread, in a sense, backwards. Declaratorsword staticdoes not contribute tothe type.)Here’s the first insight: How did I know that *x[N]is an“array of ... pointer to ...” rather than aEvery declaration in C and C++ has two“pointer to an array of ...?” It followsprincipal parts: a sequence of zero or morefrom this rule:declaration specifiers, and a sequence ofone or more declarators, separated by The operators in a declarator group accord-commas.ing to the same precedence as they do whenthey appear in an expression.For example:For example, if you check the near-est precedence chart for either C orC++, you’ll see that []has higherprecedence than *. Thus the declara-tor *x[N]means that xis an arraybefore it’s a pointer.A declarator is the name beingParentheses serve two roles indeclared, possibly surrounded bydeclarators: first, as the function calloperators such as *, [], (), and (in theoperator, and second, as grouping. Ascase of C++) &. As you already know,the function call operator, ()have thethe symbol *in a declarator meanssame precedence as []. As grouping,“pointer to” and []means “array of.”()have the highest precedence of all.
Embedded Systems Programming FEBRUARY 1999 13
PROGRAMMING POINTERS
Most of us place storage class specifiers such as staticas the first (leftmost)
declaration specifier, but it’s just a common convention, not a language
requirement.For example, *f(int)is a declaratorThe examples in your letter leadspecifying that fis a “function ...me to suspect that you may have beenreturning a pointer ... .” In contrast,tripped up by the fact that:
(*f)(int)specifies that fis a “pointerto a function ... .”
The keywords constand volatileareA declarator may contain moretype specifiers.
than one identifier. The declarator*x[N]contains two identifiers, xand N.For example, the const in:
Only one of those identifiers is theone being declared, and it’s called theconst void *vectorTable[]declarator-id. The other(s), if any,= {..<data>..};
(2)
must have been declared previously.For instance, the declarator-id indoes not apply directly to vectorTable;*x[N]is x.
it applies directly to void. This decla-A declarator need not contain anyration declares vectorTableas a vari-operators at all. In a declaration asable of type “array of pointer to constsimple as:
void.” It appears that you were expect-ing it to be “const array of pointer toint n;
void.”
Here’s yet another importantthe declarator is just the identifier ninsight:
without any operators.
The order in which the declaration speci-Declaration specifiers
fiers appear in a declaration doesn’t matter.
Some of the declaration specifiersleading up to a declarator can be typeThus, for example,
specifiers such as int, unsigned, or anidentifier that names a type. They canconst VP vectorTable[]
also be storage class specifiers such asexternor static. In C++ they can alsois equivalent to:
be function specifiers such as inlineor virtual.
VP const vectorTable[]
Here’s another insight: and
Type specifiers contribute to the type of thedeclarator-id; other specifiers provide non-const void *vectorTable[]
type information that applies directly to thedeclarator-id.is equivalent to:
For example:
void const *vectorTable[]
static unsigned long int *x[N];
Most of us place storage class speci-fiers such as staticas the first (left-declares xas a variable of type “arraymost) declaration specifier, but it’s justof Nelements of type pointer toa common convention, not a languageunsigned long int.” The keyword sta-requirement.
ticspecifies that xhas statically allo-The declaration specifiers constcated storage.
and volatileare unusual in that:
14
FEBRUARY 1999 Embedded Systems Programming
The only declaration specifiers that can alsoappear in declarators are constandvolatile.
For example, the const in:
void *const vectorTable[]
appears in the declarator. In this case,you cannot rearrange the order of thekeywords. For example:
*const void vectorTable[]
is an error.
A clarifying style
As I explained earlier, the order of thedeclaration specifiers doesn’t matterto the compiler. Therefore, these dec-larations are equivalent:
const void *vectorTable[] (3)void const *vectorTable[]
(4)
Almost all C and C++ programmersprefer to write const and volatile to theleft of the other type specifiers, as in(3). I prefer to write const and volatileto the right, as in (4), and I recom-mend it. Strongly.
Although C and C++ read mostlyfrom top-to-bottom and left-to-right,pointer declarations read, in a sense,backwards. That is, pointer declara-tions read from right-to-left. By plac-ing const to the right of the other typespecifiers, you can read pointer decla-rations strictly from right-to-left andget const to come out in the “right”places. For example:
declares pas a “pointer to a const T,”
which is exactly what it is. Also:
declares pas a “const pointer to a T,”which is also the correct interpretation.Writing const to the right of the
PROGRAMMING POINTERS
which makes it appear thatuses. Just about everyone who usesvectorTablehas type “array of pointerconst places it to the left. However,to const void.” This is wrong! The cor-given how few C and C++ program-rect interpretation is to replace VP as:mers really understand what they’re16FEBRUARY 1999 Embedded Systems Programmingdoing when it comes to using const indeclarations, “everyone else does it” ishardly an argument in favor of thecurrently popular style. Why not buckthe trend and try using a clearer style?As long as I’m on a roll here, Imight as well get in my digs in on arelated style point. Although most Cprogrammers seem to have remainedunsullied by this, many C++ program-mers have acquired the most unfortu-nate habit of writing:const int* p;rather than:const int *p;That is, they use spacing to join the *with the declaration specifiers ratherthan with the declarator. I reallybelieve C++ programmers do them-selves and each other a disservicewhen they write declarations in thisstyle. Sure, the spacing makes no dif-ference to the compiler, but puttingthe space after the *leaves many peo-ple with a false impression about theunderlying structure of declarations.Recognizing the boundary betweenthe last declaration specifier and thedeclarator is one of the keys to under-standing declarations. Breaking updeclarators with spaces this way onlyconfuses the situation.I hope I’ve answered your questionand clarified some issues.espDan Saks is the president of Saks &Associates, a C/C++ training and consult-ing company. He is also a contributing edi-tor for the C/C++ Users Journal. Heserved for many years as secretary of the C++standards committee and remains an activemember. With Thomas Plum, he wrote C++Programming Guidelines. You can writeto him at dsaks@wittenberg.edu.References1. “Static vs. Dynamic Initialization,”December 1998, p. 19.2. “Placing constin Declarations,” June1998, p. 19.