默认构造函数的概念是什么? [英] What is the concept of default constructor?

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

问题描述

帮助我获得默认构造函数的概念与示例。
我不知道什么时候在程序中使用默认构造函数,何时不使用。
帮助我解决这个问题。为我提供一个例子。
当有必要使用它吗?

 #include< iostream> 
using namespace std;

class abc
{
public:
abc()
{
cout<<hello;
}
};

int main()
{
abc a;
system(pause);
return 0;
}

所以实际上默认构造函数的使用是什么, c> c> 以下表达式(N3242的第17.6.3.1节):

  T u; //对象是默认初始化
T u {}://对象是值初始化
T(); T {}; // value initialized temporary

这个概念太多了。第12.1 / 5段实际告诉我们什么是默认构造函数


类X的默认构造函数是类X的构造函数,
可以无参数调用。如果类X没有用户声明的
构造函数,那么没有参数的构造函数是
隐式声明为默认值(8.4)。隐式声明的默认
构造函数是其类的内联公共成员。 ...


通过引入删除的特殊成员函数,标准还定义了一个没有隐式默认构造函数的情况列表以及微不足道和非平凡默认构造函数的区别。


help me in getting the concept of default constructor with example. i don't know when to use default constructor in the program and when not to. help me coming over this problem.explain it with an example for me. when it is necessary to use it?

#include<iostream>
using namespace std;

class abc
{
public:
    abc()
    {
        cout<<"hello";
    }
};

int main()
{
    abc a;
    system("pause");
    return 0;
}

so actually what is the use of default constructor and when it is necessary to use it?

解决方案

A class that conforms to the concept DefaultConstrutible allows the following expressions (paragraph 17.6.3.1 of N3242):

T u; // object is default initialized
T u{}: // object is value intialized
T(); T{}; // value initialized temporary

So much for the concept. Paragraph 12.1/5 actually tells us what a default constructor is

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4). An implicitly-declared default constructor is an inline public member of its class. ...

With the introduction of deleted special member functions, the standard also defines a list of cases where no implicit default constructor is available and the distinction of trivial and non-trivial default constructors.

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

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