码迷,mamicode.com
首页 > 编程语言 > 详细

Constants in C++

时间:2014-05-08 13:14:07      阅读:496      评论:0      收藏:0      [点我收藏+]

标签:des   class   ext   int   get   strong   

The first motivation for const seems to have been to eliminate the use of preprocessor #define for value substitution. It has since been put to use for pointers, function arguments, return types, class objects and member functions.        (Page 334)

 

1 Value substitution        (Page 334)

Because the preprocessor simply does text replacement and has no concept nor facility for type checking.

const in header files          (Page 335)

A const in C++ defaults to internal linkage; that is, it is visible only whthin the file where it is defined and cannot be seen at link time by other translation units. You must always assign a initial value to a const when you define it(but const member variable in class should not assign value when it is defined), except when you make an explicit declaration using extern.

Normally, the C++ compiler avoids creating storage for a const, but instead holds the definition in its symbol table. When you use extern with const, however, you force storage to be allocated(this is also true for certain other cases, such as taking the address of a const). 

 

2 Safety consts          (Page 336)

If you initialize a variable with a value that is produced at runtime and you know it not change for the lifetime of that variable, it is good programming practice to make it a const so the compiler will give you an error message if you accidentally try to change it.

 

3 Differences with C         (Page 338)

In C, a const always occupies srotage and its name is global. The C compiler cannot treat a const as a compile-time constant.

In C, if you say 

const int bufsize = 100;

char buf[bufsize];

you will get an error. But is OK in C++.

C defaults to external linkage for const and it always occupies srotage. So it can not contain in header file. 

 

4 Const pointers          (Page 340)

When using const with pointers, you have two options: const can be applied to what the pointer is pointing to, or the const can be applied to the address stored in the pointer itself.

pointer to const

const int * u;      (normal)

int const * u;      (rare)

const pointer

int d = 1;

int * const u = &d;

assignment and type checking          (Page 343)

C++ is very particular about type checking, and this extends to pointer assignments. You can assign the address of a non-const object to a const pointer because you‘re simply promising not to change something that is OK to change. However, you cannot assign the address of a const object to a non-const pointer because then you are saying you might change the object via the pointer.

character array literals              (Page 343)

character array literals are actually constant character arrays. If you try to change the values in a chraacter array literal, the behavior is undefined.

char * cp = "howdy";

the compiler does not allocate storage for cp;

change what pointer cp points at is undifined. If you want to be able to modify the string, put it in an array:

char cp[] = "howdy";

the compiler has allocate storage for cp.

 

5 Function arguments & return values

6 Classes

const member variables, const class object, const member function

const member variables

Inside a class, const partially reverts to its meaning in C. It allocates storage within each object and represents a value that is initialized once and then cannot change. The use of const inside a class means "This is constant for the lifetime of the object". However, we may wish that each different object contain a different value for that constant.

Thus, when you create an ordinary (non-static) const inside a class, you cannot give it an initial value. This initialization must occur in the constructor initializer list.

for example,

class Fred{

  const int size;

public:

  Fred(int sz);

};

Fred::Fred(int sz):size(sz){}

void main()

{

  Fred a(1);

}

Compile-time constants in classes

static const variable means "there is only one instance, regardless of how many objects of the class are created," which is precisely what we need here: a member of a class which is constant, and which cannot change from one object of the class to another. Thus a static const of a built-in type can be treated as a compile-time constant.

you must provide the initializer at the point of definition of the static const. As a constract, all other data member in class must be initialized in the constructor or in other member functions.

const object & const member functions

A const member function cannot modify any member variables. 

A const object is defined the same for  a user-defined type as a built-in type. And it can only call const member function.

for example,

class X{

  int i;

public:

  X(int ii);

  int f() const;

  int modify();

};

X::X(int ii):i(ii){};

X::f() const {return i;}      // the const member function can not modify data.

X::modify() { i++;}

void main()

{

  X x1(10);

  const X x2(20);

  x1.f();       // OK

  x2.f();       // OK

  x2.modify();    // error

Neither constructors nor destructors can be const member functions because they virtually always perform some modification on the object during initialization and cleanup.

 

6 volatile

it means "This data may change outside the knowledge of the compiler."

 

Constants in C++,布布扣,bubuko.com

Constants in C++

标签:des   class   ext   int   get   strong   

原文地址:http://www.cnblogs.com/ruccsbingo/p/3713982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!