c++ - 来自未定义构造函数的默认值

标签 c++ constructor

Bjarne 写道:-
对于类型 T,T() 是默认值的表示法,由默认构造函数定义。 当我们不声明默认构造函数时会发生什么?例如

using namespace std;

class date{
    int n,m;
    public:
   int day(){return n;}
   int month(){return m;}
          };//no default constructor

int main()
{
     date any =date();
     cout<<any.month()<<endl;   
     cout<<any.day()<<endl;
return 0;

}

每次运行我的程序时,该程序的输出都是 0 和 0。我没有声明任何默认构造函数那么为什么存在默认值即 0?

编辑-

    class date{
        int n,m;
        public:
        date (){
        m=1;}
       int day(){return n;}
       int month(){return m;}
     };

 int main()
  {
     date any =date();
     cout<<any.month()<<endl;   
     cout<<any.day()<<endl;
return 0;

}

阅读答案后,我提供了一个默认构造函数,但现在 n 正在获取垃圾值,但根据答案,它应该为 0,因为 m 超出了任何其他构造函数的范围,并且它是答案中提到的值初始化

最佳答案

您看到的行为是为您的类(class)定义的。


如何以及为何明确定义行为?

规则是:
如果您不提供无参数构造函数,编译器会为您的程序生成一个,以备您的程序需要。
警告:
如果您的程序为类定义任何 构造函数,编译器不会生成无参数构造函数。

根据 C++ 标准,可以通过 3 种方式初始化对象:

  • 零初始化
  • 默认初始化&
  • 值初始化

当类型名称或构造函数初始化器后跟 () 时,初始化是通过值初始化。

因此,

date any =date();
              ^^^

Value 初始化一个无名对象,然后将其复制到本地对象any, 同时:

date any;

将是一个默认初始化

值初始化为任何构造函数无法访问的成员提供零初始值。
在您的程序中,nm 超出了任何构造函数的范围,因此被初始化为 0


已编辑问题的答案:
在您编辑的案例中,您的类提供了一个无参数构造函数 date(),它能够(并且应该)初始化成员 nm , 但是这个构造函数没有初始化这两个成员,所以在这种情况下没有零初始化发生,并且对象中未初始化的成员有一个不确定(任何随机)值,进一步这个临时对象是复制到显示不确定成员值的任何对象。


致 Standerdese 粉丝:
对象初始化的规则恰本地定义在:

C++03 标准 8.5/5:

To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.

To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.

To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

关于c++ - 来自未定义构造函数的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9338881/

相关文章:

constructor - 不可变(数据)类上的多个构造函数

c++ - 清除类型为 'struct ' 的对象,没有简单的复制分配;改用赋值或值初始化

java - ArrayList不打印toString方法

java - 在 Java 运行时向面板添加元素

c++ - 使用 Clang 10 使用显式模板实例化 ~queue 的 undefined reference

c# - 在 C# 中,进行构造函数链接的最佳/可接受的方式是什么?

c++ - 在 std::map 中存储对象

C++编程题

c++ - 在 C++ 中使用 4 个交换函数修复程序

c++ - 使用 mpi 将矩阵写入单个 txt 文件