Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

c++ - What is the concept of default constructor?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...