C ++构造函数没有返回类型。只是为什么? [英] C++ Constructors have no return type. Just exactly why?

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

问题描述

我已经Googled这个和阅读许多帖子,但有这么多不同的答案,所有逻辑的感觉,我想知道是否一个专家的话题可以揭示这个问题。

I've Googled this and read many posts, but there are so many different answers that all make logical sense that I was wondering if an expert on the topic could demystify this question.

有人说没有返回,因为没有办法返回 - 语法禁止它 - 是的,这是有道理的,但我相信所有的函数都必须返回一些东西,没有?
其他人说,构造函数排序返回新创建的对象本身,这似乎有意义,因为赋值运算符在构造函数上使用。
还有其他有趣的解释。

Some say that there is no return because there is no way to return - the syntax prohibits it - yes, this makes sense, but I believe that all functions have to return something, no? Others say that the constructor sort of returns the newly created object itself, which seems to make sense since the assignment operator is used on the constructor. Still others have other interesting explanations.

推荐答案

构造函数不像其他函数一样被调用,返回类似其他函数。它们作为某些构造的副作用而执行(cast, new ,变量定义, ctor-initializer-列表返回值)。

Constructors aren't called like other functions, so they don't return like other functions. They execute as a side-effect of certain constructs (cast, new, variable definition, ctor-initializer-list, pass-by-value, return-by-value).

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

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