设为首页 加入收藏

TOP

2.3.3 Defining Objects (1)
2013-10-07 15:24:12 来源: 作者: 【 】 浏览:74
Tags:2.3.3 Defining Objects

The following statements define five variables:

  1. int units_sold;  
  2. double sales_price, avg_price;  
  3. std::string title;  
  4. Sales_item curr_book; 

Each definition starts with a type specifier, followed by a comma-separated list of one or more names. A semicolon terminates the definition. The type specifier names the type associated with the object: int, double, std::string, and Sales_item are all names of types. The types int and double are built-in types, std::string is a type defined by the library, and Sales_item is a type that we used in Section 1.5 (p. 20) and will define in subsequent chapters. The type determines the amount of storage that is allocated for the variable and the set of operations that can be performed on it.

Multiple variables may be defined in a single statement:

  1. double salary, wage;   // defines two variables of type double  
  2. int month,  
  3. day, year;             // defines three variables of type int  
  4. std::string address;   // defines one variable of type std::string 

Initialization

A definition specifies a variable’s type and identifier. A definition may also provide an initial value for the object. An object defined with a specified first value is spoken of as initialized. C++(www.cppentry.com) supports two forms of variable initialization:
copy-initialization and direct-initialization. The copy-initialization syntax uses the equal (=) symbol; direct-initialization places the initializer in parentheses:

  1. int ival(1024); // direct-initialization  
  2. int ival = 1024; // copy-initialization 

In both cases, ival is initialized to 1024.

本书会反复提到拷贝初始化和直接初始化两个术语,值得印在脑中。另外,拷贝初始化不是赋值。

Although, at this point in the book, it may seem obscure to the reader, in C++(www.cppentry.com) it is essential to understand that initialization is not assignment. Initialization happens when a variable is created and gives that variable its initial value. Assignment involves obliterating an object’s current value and replacing that value with a new one.

Many new C++(www.cppentry.com) programmers are confused by the use of the = symbol to initialize a variable. It is tempting to think of initialization as a form of assignment. But initialization and assignment are different operations in C++(www.cppentry.com). This concept is particularly confusing because in many other languages the distinction is irrelevant and can be ignored. Moreover, even in C++(www.cppentry.com) the distinction rarelymatters until one attempts to write fairly complex classes. Nonetheless, it is a crucial concept and one that we will reiterate throughout the text.

There are subtle differences between copy- and direct-initialization when initializing objects of a class type. We won’t completely explain these differences until Chapter 13. For now, it’s worth knowing that the direct syntax is more flexible and can be slightly more efficient.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.3.3 Defining Objects (2) 下一篇2.1.1 Integral Types (4)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)