博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
conversion constructor
阅读量:4285 次
发布时间:2019-05-27

本文共 2696 字,大约阅读时间需要 8 分钟。

Ref  Q16

If a class has a constructor which can be called with a single argument, then this constructor becomes conversion constructor because such a constructor allows automatic conversion to the class being constructed. A conversion constructor can be called anywhere when the type of single argument is assigned to the object. The output of the given program is

CalledCalled
#include 
using namespace std;class Test{private: int x;public: Test(int i) { x = i; cout << "Called" << endl; }};int main(){ Test t(20); t = 30; // conversion constructor is called here. return 0;}

 

ref

The definition for a converting constructor is different between C++03 and C++11. In both cases it must be a non-explicit constructor (otherwise it wouldn't be involved in implicit conversions), but for C++03 it must also be callable with a single argument. That is:

 
struct foo{
foo(int x); // 1 foo(char* s, int x = 0); // 2 foo(float f, int x); // 3 explicit foo(char x); // 4 };
 

Constructors 1 and 2 are both converting constructors in C++03 and C++11. Constructor 3, which must take two arguments, is only a converting constructor in C++11. The last, constructor 4, is not a converting constructor because it is explicit.

 
  • C++03: §12.3.1

    A constructor declared without the function-specifier explicit that can be called with a single parameter specifies a conversion from the type of its first parameter to the type of its class. Such a constructor is called a converting constructor.

  • C++11: §12.3.1

    A constructor declared without the function-specifier explicit specifies a conversion from the types of its parameters to the type of its class. Such a constructor is called a converting constructor.

 

Why are constructors with more than a single parameter considered to be converting constructors in C++11? That is because the new standard provides us with some handy syntax for passing arguments and returning values using braced-init-lists. Consider the following example:

 
foo bar(foo f) { return { 1.0f, 5}; }
 

The ability to specify the return value as a braced-init-list is considered to be a conversion. This uses the converting constructor for foo that takes a float and an int. In addition, we can call this function by doing bar({2.5f, 10}). This is also a conversion. Since they are conversions, it makes sense for the constructors they use to be converting constructors.

 

It is important to note, therefore, that making the constructor of foo which takes a float and an int have the explicit function specifier would stop the above code from compiling. The above new syntax can only be used if there is a converting constructor available to do the job.

转载地址:http://gesgi.baihongyu.com/

你可能感兴趣的文章
matlab读取UCI中获取的.data文件
查看>>
matlab错误:Subscript indices must either be real positive integers or logicals.
查看>>
行列式及其性质
查看>>
matlab 保留固定长度的整数位
查看>>
xshell-常用命令
查看>>
用xshell运行matlab 远程给Linux服务器安装Matlab R2014b
查看>>
在本地电脑使用远程服务器的图形界面——包括 MATLAB、PyCharm 等各种软件
查看>>
向量转置怎么求导(多元线性回归原理推导用)
查看>>
Matlab中布尔值/逻辑值与数值型类型的相互转换
查看>>
Matlab 并行代码
查看>>
matlab中的并行方法与理解(2):parfor中的变量类型
查看>>
CentOS 7 命令行模式安装teamviewer13
查看>>
teamviewer Linux centos7安装使用详细
查看>>
【MATLAB】线条标记符大小设置
查看>>
MATLAB中矩阵的逻辑索引方法
查看>>
windows下go dep环境搭建
查看>>
EMQX docker安装及运行
查看>>
使用python和MQTT.fx连接mqtt
查看>>
EMQTT的ACL鉴权(topic权限控制)
查看>>
emqx客户端用户名密码登录验证配置
查看>>