设为首页 加入收藏

TOP

5.11 The new and delete Expressions (1)
2013-10-07 15:24:06 来源: 作者: 【 】 浏览:70
Tags:5.11 The new and delete Expressions

In Section 4.3.1 (p. 134)we sawhow to use new and delete expressions to dynamically allocate and free arrays. We can also use new and delete to dynamically allocate and free single objects.

When we define a variable, we specify a type and a name. When we dynamically allocate an object, we specify a type but do not name the object. Instead, the new expression returns a pointer to the newly allocated object; we use that pointer to access the object:

  1. int i; // named, uninitialized int variable  
  2. int *pi = new int// pi points to dynamically allocated,  
  3. // unnamed, uninitialized int 

This new expression allocates one object of type int from the free store and returns the address of that object. We use that address to initialize the pointer pi.

Initializing Dynamically Allocated Objects

Dynamically allocated objects may be initialized, in much the same way as we initialize variables:

  1. int i(1024); // value of i is 1024  
  2. int *pi = new int(1024); // object to which pi points is 1024  
  3. string s(10, ’9’); // value of s is "9999999999"  
  4. string *ps = new string(10, ’9’); // *ps is "9999999999" 

We must use the direct-initialization syntax (Section 2.3.3, p. 48) to initialize dynamically allocated objects. When an initializer is present, the new expression allocates the required memory and initializes that memory using the given initializer(s). In the case of pi, the newly allocated object is initialized to 1024. The object pointed to by ps is initialized to a string of 10 nines.

Default Initialization of Dynamically Allocated Objects

If we do not explicitly state an initializer, then a dynamically allocated object is initialized in the same way as is a variable that is defined inside a function. (Section 2.3.4, p. 50) If the object is of class type, it is initialized using the default constructor for the type; if it is of built-in type, it is uninitialized.

  1. string *ps = new string; // initialized to empty string  
  2. int *pi = new int// pi points to an uninitialized int 

As usual, it is undefined to use the value associated with an uninitialized object in any way other than to assign a good value to it.

Just as we (almost) always initialize the objects we define as variables, it is (almost) always a good idea to initialize dynamically allocated objects.

We can also value-initialize (Section 3.3.1, p. 92) a dynamically allocated object:

  1. string *ps = new string(); // initialized to empty string  
  2. int *pi = new int(); // pi points to an int value-initialized to 0  
  3. cls *pc = new cls(); // pc points to a value-initialized object of type cls 

We indicate that we want to value-initialize the newly allocated object by following the type name by a pair of empty parentheses. The empty parentheses signal that we want initialization but are not supplying a specific initial value. In the
case of class types (such as string) that define their own constructors, requesting value-initialization is of no consequence: The object is initialized by running the default constructor whether we leave it apparently uninitialized or ask for valueinitialization. In the case of built-in types or types that do not define any constructors, the difference is significant:

这段话是说,如果new的对象定义了构造函数,那么加不加后面那对圆括号()并不影响程序的行为;如果new 的对象没有构造函数,或者是内置类型,那么就大不一样了。

  1. int *pi = new int// pi points to an uninitialized int  
  2. int *pi = new int(); // pi points to an int value-initialized to 0 

In the first case, the int is uninitialized; in the second case, the int is initialized to zero.

The () syntax for value initialization must follow a type name, not a variable. As we’ll see in Section 7.4 (p. 251)

  1. int x(); // does not value initialize x 

declares a function named x with no arguments that returns an int.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.11 The new and delete Express.. 下一篇5.12.5 When Casts Might Be Usef..

评论

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

·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)