C++ 之 Direct and Copy Forms of Initialization

Extraction from C++ Primer 5th. Editioin 3.2.1

C++ has several different forms of initialization, we should understand how these forms differ from one aother.

When we initialize a variable using =, we are asking the compiler to copy initialize the object by copying the initializer on the right-hand side into the object being created.

Ohterwise when we omit the =, we use direct intialization.When we have a single intializer, we use either the direct or copy form of intialization. When we intialize a variable from more than one value, such as:

string s4(10, ‘c‘);

we must use the direct form of initialization.

when we want to use several values, we can indirectly use the copy form of intialization be explicitly creating a (temporary) object to copy:

string s8=string(10, ‘c‘);    //copy initialization

The intializer of s8——string(10, ‘c‘)——creates a string of the given size and character value and then copies that value into s8. it is as if we had written

string temp(10, ‘c‘);
string s8=temp;

Although the used to intialize s8 is legal, it is less readable and offers no compensating advantage over the way we intilize s4.

时间: 2024-10-19 09:53:50

C++ 之 Direct and Copy Forms of Initialization的相关文章

Lock-less and zero copy messaging scheme for telecommunication network applications

A computer-implemented system and method for a lock-less, zero data copy messaging mechanism in a multi-core processor for use on a modem in a telecommunications network are described herein. The method includes, for each of a plurality of processing

Lazy Initialization with Swift

Lazy initialization (also sometimes called lazy instantiation, or lazy loading) is a technique for delaying the creation of an object or some other expensive process until it's needed. When programming for iOS, this is helpful to make sure you utiliz

C++ Core Guidelines

C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very early draft. It is inkorrekt, incompleat, and pµøoorly formatted. Had it been an open source (code) project, this would have been release 0.6. Copy

【转】Dalvik虚拟机的启动过程分析

在Android系统中,应用程序进程都是由Zygote进程孵化出来的,而Zygote进程是由Init进程启动的.Zygote进程在启动时会创建一个Dalvik虚拟机实例,每当它孵化一个新的应用程序进程时,都会将这个Dalvik虚拟机实例复制到新的应用程序进程里面去,从而使得每一个应用程序进程都有一个独立的Dalvik虚拟机实例.在本文中,我们就分析Dalvik虚拟机在Zygote进程中的启动过程. 老罗的新浪微博:http://weibo.com/shengyangluo,欢迎关注! Zygot

MQX学习笔记(二)

基于MQX的应用编程: 通过任务模板定义用户任务,MQX_AUTO_START_TASK -- 任务随MQX一起启动: /*MQX通过下面的代码(任务模板),向系统注册多个用户进程:--- 当然,我们也可以通过MQX的接口函数来创建任务.*/ const TASK_TEMPLATE_STRUCT MQX_template_list[] = { /* Task Index, Function, Stack, Priority, Name, Attributes, Param, Time Slice

Oracle Applications Multiple Organizations Access Control for Custom Code

文档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code Checked for relevance on 12-JAN-2011 See Change Record This document discusses how to update the customization code that is affected by the access co

Maven Web项目配置Mybatis出现SqlSessionFactory错误的解决方案

一.错误现象 [html] view plain copy 严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in URL [file:/F:/Workspaces/.metadata

2014 appliance business revenue growth of 302% ??HAILAN advance O2O-Replica Oakley Sunglasses

March 11 message, the reporter discovered that a couple of days ago, men's brand Sea Orchid Residence announced the 2014 Annual Report - income of 12.338 billion yuan, a rise of 72.56%. The report shows that in 2015 Sea Orchid Residence will accelera

[转]Publishing and Running ASP.NET Core Applications with IIS

本文转自:https://weblog.west-wind.com/posts/2016/Jun/06/Publishing-and-Running-ASPNET-Core-Applications-with-IIS#DoyouneedIIS? When you build ASP.NET Core applications and you plan on running your applications on IIS you'll find that the way that Core ap