调用默认构造函数

编程入门 行业动态 更新时间:2024-10-11 17:23:50
本文介绍了调用默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个在C ++中的默认构造函数的问题。这是一个简单的事情,但不能看到它有什么问题。

I have a problem with a default constructor in C++. It's a simple thing but can't see what's wrong with it.

我有一个构造函数有3个可选参数,初始化列表中的const值:

I have a constructor with 3 optional parameters, with const values on initialization list:

data::data(int D = 1, int M = 1, int Y = 1583) : Day(D), Month(M), Year(Y) { if (!CorrectDate()) throw "Wrong Date!"; }

为什么我可以用一个,两个或三个参数调用它,好的,但是当我没有参数调用它时不会?

Why can I call it with one, two or three parameters and it works just fine but doesn't when I call it with no parameters?

data tommorrow();

推荐答案

data tomorrow 是返回数据的函数的声明,并且不带参数。要创建一个没有显式构造函数参数的对象,只需要明天执行数据;不带括号的。

data tomorrow(); is a declaration of a function that returns a data and takes no parameters. To create a data object with no explicit constructor arguments, just do data tomorrow; without the parentheses.

更多推荐

调用默认构造函数

本文发布于:2023-11-12 09:55:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!