About default constructor
-
wrote on 27 Jul 2011, 15:16 last edited by
@
class A {
public:
A(int x = 0) { v = x; }
private:
int v;
};
@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? -
wrote on 27 Jul 2011, 15:38 last edited by
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
@class A {
public:
A();
...
@ -
wrote on 27 Jul 2011, 15:46 last edited by
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.
-
wrote on 27 Jul 2011, 18:18 last edited by
bq.
"ISO/IEC 14882:2003, 12.1.5":http://cs.nyu.edu/courses/summer11/G22.2110-001/documents/c++2003std.pdf 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.[quote author="Edico" date="1311779819"]Is A(int x = 0); a default constructor?[/quote]
Yes.
-
wrote on 27 Jul 2011, 18:51 last edited by
It is a default constuictor as the argument is optional.
-
wrote on 27 Jul 2011, 20:40 last edited by
[quote author="Edico" date="1311779819"]The definition for default constructor: "The constructor that takes no arguments is known as the default constructor."[/quote]
The definition is wrong.
-
wrote on 28 Jul 2011, 07:11 last edited by
bq. 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 :
@class A { public:
//A(int x = 0) { v = x; cout << " test my constructor";} //just for later use
private:
int v;
}; @So for instance when you use class A in another class B, the default constructor, implicitly created by the compiler, will be called.
@class B { public:
private:
A a;
}; @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 ;)
-
wrote on 8 Aug 2011, 10:28 last edited by
it's just a defination...the default constructor is always one without parameters (included optional parameters)
-
wrote on 8 Aug 2011, 10:51 last edited by
The C++ standard is very clear about what a default constructor is (section 12.1, number 5):
[quote]
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. [...]
[/quote]