构造函数返回什么? [英] What does a constructor return?

查看:177
本文介绍了构造函数返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是构造函数返回什么?这个问题与什么是构造函数的返回类型是什么?
我在某处读到一个构造函数返回一个完整的对象隐式(即隐式返回类型是类的名称),但不应明确指定。

My question is what does a constructor return? This question is not quite different from "What is the return type of a constructor?" I have read somewhere that a constructor returns a complete object implicitly (i.e implicit return type is the name of the class) but it shall not be specified explicitly.

struct empty{};

int main(){
   empty(); //creates a temporary and implicitly a constructor is called
}

隐式返回类型应该是类的名称,在这种情况下 empty

So as per my interpretation the implicit return type should be the name of the class, in this case empty. Is my wild interpretation correct?

推荐答案

构造函数不返回任何内容。调用构造函数来初始化对象。构造函数只能用于初始化对象;你不能实际调用一个构造函数显式(一件事,构造函数没有名称)。

A constructor doesn't return anything. A constructor is called to initialize an object. A constructor can only be used to initialize an object; you can't actually call a constructor explicitly (for one thing, constructors do not have names).

在你给的例子中, empty )不是一个函数调用表达式,它是值初始化。它创建一个类型为 empty 的值初始化的临时对象。

In the example you give, empty() is not a function call expression, it is value initialization. It creates a value-initialized temporary object of type empty.

这篇关于构造函数返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆