Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Okay I got a pretty simple assignment.

I got these two constructors for class Person:

Person( const string &, const string &, const string & );
Person( const string &, const string &, const string &,
const string & );

I got 4 default values

which of these are going to be the default constructor? is it always the one with most arguments or how does it work?

See Question&Answers more detail:os

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

1 Answer

According to the C++ Standard

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

From your post it is not clear what default values you are speaking about. Neither of your declarations is the default constructor.

If you are speaking about default arguments as in the declaration

Person( const string & = "", const string & = "", const string & = "",
const string & = "" );

Then this declaration is a declaration of the default constructor because it can be called without any explicitly specified argument.

It is interesting to note that the same constructor can be a default constructor and a non-default constructor at the same time. At least the C++ Standard does not say anything that forbids this.

For example

struct A
{
   A( int x );
   int x;
};

A a1; // error: there is no default constructor

A::A( int x = 0 ) : x( x ) {}

A a2; // well-formed there is a default constructor.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...