gpt4 book ai didi

c++ - 为什么默认参数构造函数被称为默认构造函数

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:15 25 4
gpt4 key购买 nike

Class A {
public:
A(int i = 0, int k = 0) {} // default constructor WHY ??
~A() {}
};
int main()
{
A a; // This creates object using defined default
// constructor but the constructor still has two arguments
A b(1,2); // Called as parametrized one
}

为什么这个默认参数构造函数是默认构造函数。为什么它不被称为参数化构造函数或默认参数化构造函数,因为即使这个构造函数在没有参数的情况下被调用,它也包含两个参数?是否有任何特定原因或仅仅是因为标准如此规定。

最佳答案

C++11 §12.1 Constructors

A default constructor for a class X is a constructor of class X that can be called without an argument.

这是默认构造函数的定义。为其所有参数提供默认参数的构造函数可以在没有参数的情况下调用,因此符合定义。

关于c++ - 为什么默认参数构造函数被称为默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59309058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com