调用默认构造函数 [英] Calling default constructor
问题描述
我有一个在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.
这篇关于调用默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!