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

C++ Variables and Basic Types Notes

时间:2014-10-03 18:53:04      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:des   io   os   ar   for   sp   art   c   on   

1. Type conversion: 

If we assign an out-of-range value to an object of unsigned type, the result is the remainder of the value modulo the number of values the target type can hold. 

If we assign an out-of-range value to an object of signed type, the result is undefined.

2.What does a variable mean?

A variable provides us a named storage that our program can manipulate.

3.Variable initialization and assignment

Initialization happens when a variable is given a value when it is created, whereas assignment is to replace the old value of the variable with new value.

4.Variable declaration and definition

To support seperate compilation, C++ distinguishes declaration and definition. A declaration makes a name known to the program, especially when a program wants to use a variable that is defined in other file. The definition creates the associated entity. 

To get a declaration that is not a definition, we can add the ‘extern‘ word before variable and not provide an initial value.

P.S. Variable must be defined exactly once but can be declared many times.

5. Reference and pointer

Reference defines an alternative name for an object. A reference must be initialized. And once initialized, a reference remains bound to its initial object. There is no way to rebind a reference to a different object.

Pointer is a type that stores the address of an object. Unlike reference, a pointer needn‘t to  be initialized when it‘s created. And a pointer can point to differnent object in its lifetime.

P.S. Reference is not an object. Hence, we may not have a pointer to a reference. Also, there is ‘const pointer‘ but no ‘const reference‘.

6. void * pointer

The type void * is a special pointer type that can hold address of any type of object. 

7. Const

We can make a variable unchangeable by defining the variable‘s type as const. Because we can‘t change the value of a const object after we create it, so we need to initialize it when creting it.

P.S. To share a const object across multiple files, you must define the variable as extern.

Const pointer: we indicate the pointer is const by putting the ‘const‘ after ‘*‘.

8. Auto type specifier

Under the new standard, we can let the compiler to figure out the type of an expression by using the auto type specifier. The variable using auto as its type specifier must have its initializer. 

In particular, when we use the reference as the initializer, the initializer is the corresponding object. Second, auto ordinarily ignores top-level const.

9. Decltype type specifier

Sometimes we want to define a variable with the type that the compiler deduces from an expression but not want to use the expression to initialize the variable. For such case, the new standard introduce the ‘decltype‘ specifier, which returns the type of its operand.

 

C++ Variables and Basic Types Notes

标签:des   io   os   ar   for   sp   art   c   on   

原文地址:http://www.cnblogs.com/Kai-Xing/p/4005005.html

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