C++ supports different forms of explicit initialization.

  • In The C++ Standard Library page 14, it produced “explicit initialization”which is useful for template
    template <class T>
    void f( )
    {
    T x = T( );
    ...
    }

    x will be initialized as 0

    also:   int a = int( );  a will be initialized as 0

  • Supplying an initializer list in parentheses:

    String sFileName( "FILE.DAT" );
    

    The items in the parenthesized list are considered arguments to the class constructor. This form of initialization enables initialization of an object with more than one value and can also be used in conjunction with the new operator. For example:

    Rect *pRect = new Rect( 10, 15, 24, 97 );
    

  • Supplying a single initializer using the equal-sign initialization syntax. For example:

    String sFileName = "FILE.DAT";
    

    Although the preceding example works the same way as the example shown for String in the first list item, the syntax is not adaptable to use with objects allocated on the free store.

    The single expression on the right of the equal sign is taken as the argument to the class's copy constructor; therefore, it must be a type that can be converted to the class type.

    Note that because the equal sign (=) in the context of initialization is different from an assignment operator, overloading operator= has no effect on initialization.

The equal-sign initialization syntax is different from the function-style syntax, even though the generated code is identical in most cases. The difference is that when the equal-sign syntax is used, the compiler has to behave as if the following sequence of events were taking place:

  • Creating a temporary object of the same type as the object being initialized.

  • Copying the temporary object to the object.

The constructor must be accessible before the compiler can perform these steps. Even though the compiler can eliminate the temporary creation and copy steps in most cases, an inaccessible copy constructor causes equal-sign initialization to fail (under /Za, /Ze (Disable Language Extensions)). Consider the following example:

// spec1_explicit_initialization.cpp
// compile with: /Za
class anInt {anInt( const anInt &copy ) {}   // private copy constructorpublic:anInt( int ) {}   // public constructor
};int main() {// Access-control violation. // Attempt to reference private copy constructor.anInt myInt = 7;   // C2248anInt myInt2(7);   // Correct; no copy constructor called.
}

When a function is called, class-type arguments passed by value and objects returned by value are conceptually initialized using the form:

type-name name = value

For example:

String s = "C++";

Therefore, it follows that the argument type must be a type that can be converted to the class type being passed as an argument. The class's copy constructor, as well as user-defined conversion operators or constructors that accept the type of the actual argument, must be public.

In expressions that use the new operator, the objects allocated on the free store are conceptually initialized using the form:

type-name name( initializer1, initializer2, ... initializer)

For example:

String *ps = new String( "C++" );

Explicit Initialization相关推荐

  1. Inside C++ object Model--构造函数

    默认构造函数 构造函数是干啥的, 是在构造类对象的时候, 给程序员进行对象初始化操作的机会. 不仅如此, 同时也是给编译器进行对象初始化的机会. 当然程序员和编译器的扮演的角色是不一样的, 考虑的问题 ...

  2. 深入探索C++对象模型学习笔记2

    密码管理请下载: http://a.app.qq.com/o/simple.jsp?pkgname=com.wa505.kf.epassword 1.   关于对象 1.1.  虚表: 1.1.1.  ...

  3. 深入探索C++对象模型学习笔记

    密码管理请下载: http://a.app.qq.com/o/simple.jsp?pkgname=com.wa505.kf.epassword 1.   关于对象 1.1.  虚表: 1.1.1.  ...

  4. java构造方法可以重载吗_Java基础教程之构造器与方法重载

    在方法与数据成员中,我们提到,Java中的对象在创建的时候会初始化(initialization).初始化时,对象的数据成员被赋予初始值.我们可以显式初始化.如果我们没有给数据成员赋予初始值,数据成员 ...

  5. 《深度探索C++对象模型》--5 构造析构拷贝 6 执行期语意学

     <深度探索C++对象模型>--5构造.析构.拷贝语意学 1.纯虚函数: (1)C++可以定义和调用一个纯虚函数,不过只可以静态调用,不可以由虚拟机制调用. 注意:pure virtu ...

  6. Java基础02 方法与数据成员

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在Java基础01 从HelloWorld到面向对象,我们初步了解了对象(obje ...

  7. Java基础03 构造器与方法重载

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在方法与数据成员中,我们提到,Java中的对象在创建的时候会初始化(initial ...

  8. 编程书说的“Go程序员应该让聚合类型的零值也具有意义”是在讲什么

    在<Go语言编程>这本书和很多其他Go 编程教程中很多都提到过"Go程序员应该让一些聚合类型的零值也具有意义"的概念,我们这篇文章主要说一下有意义的零值这个话题. 在 ...

  9. inside the C++ Object model总结

    一. 关于对象 1.内联函数:能够除去函数调用的开支,每一处内联函数的调用都是代码的复制.这是一种空间换取时间的做法,若函数代码量大或者有循环的情况下,不宜内联(这件事有些编译器会自动帮你做).在类中 ...

最新文章

  1. 【邮箱使用】Yahoo邮箱POP3、SMTP开通以及设置方法
  2. 程序可以下载,在线调试disassembly窗口就出现了大片0x00000000 FFFFFFFF DCD 0xFFFFFFFF ; ? Undefined
  3. “电梯演讲”最精炼、贴切的语言
  4. qt的输出中文,数字到表格
  5. JAVA基础知识(2)
  6. css实战手册第四版 pdf_你真的了解CSS继承吗?看完必跪
  7. 矩阵可逆的一种刻画方式
  8. unity获取电磁笔压感_【WPF】获取电磁笔的压感
  9. Mac安装MATLAB 2017b
  10. Hive 使用问题集锦
  11. 一个简单的问题,为什么我要搞得复杂绕一大圈
  12. 20.6.5算法心得 一元二次方程解法
  13. 【C++】std::numeric_limits 类型对应最值查询
  14. Linux中使用sendmail发送邮件,指定任意邮件发送人
  15. 1265 最近公共祖先
  16. 学习Android的一些网站
  17. 输入一行字符,分别统计出其数字、大写字母、小写字母和其他字符的个数。
  18. 【原创题】皮卡丘的兄弟姐妹
  19. 【Linux】一文简单了解操作系统在硬件中的作用,解析操作系统是做什么的?
  20. 基于Zemax平台的激光扩束系统的设计

热门文章

  1. 以太坊智能合约(模拟积分兑换商品)
  2. Django + Celery 实现周期任务
  3. 【博学谷学习记录】超强总结,用心分享 |JSON
  4. JVM虚拟机内存划分易懂图解
  5. 能贴在iPhone桌面上的跨平台便签工具
  6. 银行营销方案计算机实验心得体会,浙科电子商务实验报告总结.docx
  7. Vue路由跳转与接收参数
  8. 深度神经网络的工作原理,深度神经网络基本原理
  9. 【BZOJ1997】【HNOI2010】Planar(2-SAT,平面图,并查集)
  10. TCP/IP五层协议