未定义构造函数的默认值 [英] default values from not defined constructor

查看:142
本文介绍了未定义构造函数的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bjarne写道:

对于类型T,T()是默认构造函数
当我们不声明默认构造函数时会发生什么?例如

  using namespace std; 

class date {
int n,m;
public:
int day(){return n;}
int month(){return m;}
}; //无默认构造函数

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

}

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



EDIT -

 类别日期{
int n ,m;
public:
int(){
int(){
date(){
m = 1;}
$ b};

int main()
{
date any = date();
cout<< any.month()<< endl;
cout<< any.day()<< endl;
return 0;在阅读答案后,我提供了一个默认的构造函数,但现在n是一个默认的构造函数。获取垃圾值,但根据答案应该为0,因为m不在任何其他构造函数的范围,并且它是如在回答

中提到的值初始化

解决方案

您看到的行为是您班级的定义良好。






为什么行为定义明确?



规则为:

如果您不提供一个无参数构造函数,编译器为您的程序生成一个程序,以防您的程序需要一个。

警告:

编译器不生成无参构造函数如果您的程序为类定义了任何构造函数。



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




  • 零初始化

  • 默认初始化&

  • 价值初始化


$ b b

当类型名或构造函数初始化程序后面跟有()时,初始化是通过值初始化。



因此,

  date any = date 
^^^

值初始化将其复制到本地对象任何
while:

 <$ c $日期any; 

将是默认初始化



值初始化对于任何构造函数无法访问的成员,初始值为零。

在程序中, n m 超出了任何构造函数的范围,因此被初始化为 0

,你的类提供了一个没有参数的构造函数 date(),它能够(& should)初始化成员 n m ,但是这个构造函数不会初始化两个成员,所以在这种情况下不会发生零初始化,并且对象中的未初始化成员具有 Indeterminate (任何随机)值,此临时对象将复制到任何对象,显示不确定的成员值。






对于Standerdese粉丝:

对象初始化的规则适当地定义在:



C ++ 03 Standard 8.5 / 5:


>零初始化类型T的对象意味着:

- 如果T是标量类型(3.9),则对象设置为转换为T的值0(零) br>
- 如果T是非联合类类型,则每个非静态数据成员和每个基类子对象都是零初始化的;

- 如果T是联合类型,则对象的第一个命名数据成员是零初始化的;

- 如果T是数组类型,每个元素都是零初始化的;

- 如果T是引用类型,则不执行初始化。



默认初始化类型T的对象意味着:

- 如果T是非POD类类型(第9节),调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化不成功);

- 如果T是数组类型,则每个元素都是默认初始化的;

- 否则,对象是零初始化的。



值初始化类型T的对象意味着:

- 如果T是类类型)和用户声明的构造函数(12.1),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化不成功);

- 如果T是非没有用户声明的构造函数的联合类类型,则T的每个非静态数据成员和基类组件都被初始化;

- 如果T是数组类型,则每个元素是值 - 已初始化;

- 否则,对象已初始化为零



Bjarne wrote:-
For a type T, T() is the notation for the default value , as defined by the default constructor . What happen when we don't declare default constructor ? For example

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;

}

Output of this program is 0 and 0 every time i run my program. I haven't declare any default constructor then why there exits a default value i.e. 0?

EDIT-

    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;

}

After reading answers i provide a default constructor but now n is getting garbage value but according to answers it should be 0 as m is out of reach of any other constructor and it is value initialisation as mentioned in answer

解决方案

The behavior you see is Well-Defined for your class.


How & Why is the behavior Well-Defined?

The rule is:
If you do not provide a no argument constructor the compiler generates one for your program in case your program needs one.
Caveat:
The compiler does not generate the no argument constructor if your program defines any constructor for the class.

As per the C++ Standard an object can be initialized in 3 ways:

  • Zero Initialization
  • Default Initialization &
  • Value Initialization

When, a type name or constructor initializer is followed by () the initialization is through value initialization.

Thus,

date any =date();
              ^^^

Value Initializes an nameless object and then copies it in to the local object any, while:

date any;

would be a Default Initialization.

Value Initialization gives an initial value of zero to members that are out of reach of any constructor.
In your program, n and m are beyond the reach of any constructor and hence get initialized to 0.


Answer to Edited Question:
In your edited case, your class provides a no argument constructor, date(), which is capable(& should) initialize members n and m, but this constructor doesn't initialize both the members, So In this case no zero initialization takes place, and the uninitialized members in the object have an Indeterminate(any random) value, further this temporary object is copied to any object which displays the shows indeterminate member values.


For Standerdese Fans:
The rules for object Initialization are aptly defined in:

C++03 Standard 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

这篇关于未定义构造函数的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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