July 27, 2011

Edico Edico
Lab Rat
31 posts

About default constructor

 

  1. class A {
  2. public:
  3.     A(int x = 0) { v = x; }
  4. private:
  5.     int v;
  6. };

The definition for default constructor: “The constructor that takes no arguments is known as the default constructor.”
Is A(int x = 0); a default constructor?

9 replies

July 27, 2011

HuXiKa HuXiKa
Lab Rat
83 posts

As you just said, “The constructor that takes no arguments is known as the default constructor.”
The A(int x = 0) constructor takes int x as an argument, so no, it isn’t a default constructor. The default constructor would be

  1. class A {
  2. public:
  3.    A();
  4. ...

 Signature 

If you can find faults of spelling in the text above, you can keep them.

July 27, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

You can test it yourself. Put a debug in the constructor. Create an instance of A and see if your constructor is called or not.

 Signature 

Qt Certified Specialist
Qt Ambassador

July 27, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

ISO/IEC 14882:2003, 12.1.5 [cs.nyu.edu] 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 default constructor is implicitly declared.

Edico wrote:
Is A(int x = 0); a default constructor?

Yes.

July 27, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

It is a default constuictor as the argument is optional.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

July 27, 2011

peppe peppe
Ant Farmer
1025 posts
Edico wrote:
The definition for default constructor: “The constructor that takes no arguments is known as the default constructor.”

The definition is wrong.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.4

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

July 28, 2011

Chuck Gao Chuck Gao
Lab Rat
342 posts

  1. A(int x = 0)

is a default constructor with the default argument.

if

  1. A(int x)
without default argument, it’s not a default constructor

 Signature 

Chuck

July 28, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

You can test it yourself. Put a debug in the constructor. Create an instance of A and see if your constructor is called or not.

The practical meaning of a default constructor is that if you don’t define one yourself the compiler is making one implicitly for you.

Have a look at this example :

  1. class A { public:    
  2.    //A(int x = 0) { v = x; cout << " test my constructor";}    //just for later use
  3.    private:    
  4.        int v;
  5. };

So for instance when you use class A in another class B, the default constructor, implicitly created by the compiler, will be called.

  1. class B { public:    
  2.      private:    
  3.         A a;
  4. };

If you uncomment line 2 of class A, you will see the cout message, meaning your constructor is called with no parameter. Thus it’s a default constructor. And you as a programmer took control over it.

In this example there is no practical need to define one since the result of both will be te same. Of course in real life things aren’t that simple ;)

 Signature 

Qt Certified Specialist
Qt Ambassador

August 8, 2011

AlterX AlterX
Ant Farmer
123 posts

it’s just a defination…the default constructor is always one without parameters (included optional parameters)

August 8, 2011

Volker Volker
Robot Herder
5428 posts

The C++ standard is very clear about what a default constructor is (section 12.1, number 5):

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 default constructor is implicitly declared. […]
 
  ‹‹ How can i inside enum in class      [SOLVED] [Functions returning pointers] Accessing class methods from base class pointers ››

You must log in to post a reply. Not a member yet? Register here!