Default parameters with C++ constructors
Definitely a matter of style. I prefer constructors with default parameters, so long as the parameters make sense. Classes in the standard use them as well, which speaks in their favor.
One thing to watch out for is if you have defaults for all but one parameter, your class can be implicitly converted from that parameter type. Check out this thread for more info.
C++ constructor with one default parameter and one variable parameter
This declaration of the constructor is invalid:
element(int f=0, int a)
{
first = f;
inc = a;
current = first - inc;
}
If a parameter has a default argument, all subsequent parameters are also required to have a default argument.
What you need is to declare two constructors, like for example:
element(int f, int a) : first( f ), inc( a )
{
current = first - inc;
}
element(int a) : element( 0, a )
{
}
It is desirable to declare the second constructor as explicit
to prevent implicit conversions from a single integer to the element
type:
explicit element(int a) : element( 0, a )
{
}
How do I specify default argument values for a C++ constructor?
What you need is:
//declaration:
MyConstuctor(int inDenominator, int inNumerator, int inWholeNumber = 0);
//definition:
MyConstuctor::MyConstuctor(int inDenominator,int inNumerator,int inWholeNumber)
{
mNum = inNumerator;
mDen = inDenominator;
mWhole = inWholeNumber;
}
This way you will be able to provide a non-default value for inWholeNumber
; and you will be able not to provide it so 0 will be used as the default.
As an additional tip, better use initialization list in the definition:
//definition:
MyConstuctor::MyConstuctor(int inDenominator,int inNumerator,int inWholeNumber) :
mNum(inNumerator), mDen(inDenominator), mWhole (inWholeNumber)
{
}
Default values of arguments in C++ constructor
I consider both approaches to be equally valid. Depending on the particulars of each individual class, each approach has its advantages, strengths, and weaknesses.
Using a default value would typically be the best choice when the object needs to be initialized pretty much the same way, whether or not the constructor parameter gets defaulted. Specifying a default value prevents the need to duplicate a bunch of code. You only have one constructor.
On the other hand, using overloaded constructors makes it possible to cleanly construct the object in completely different way, depending on whether the parameter is given or not. Forcing the class to have a single constructor typically results in carpetbombing the code with a bunch of if
statements, in this case.
Also, don't forget about the third option: a delegating constructor. Using the class in your example code:
Object() : Object("")
{
}
This approach has its own inherent advantages too.
There is no consensus on which approach is best, in general. It's best to consider the individual requirements of each class, and choose the approach that works best for that class. What's best for one class may not be the best way for another class.
In c++, if I create a constructor that takes one argument which has a default value - will that serve as a default (empty) constructor?
Yes, the definition of default constructor allows parameters as long as they have default values:
A default constructor for a class
X
is a constructor of classX
for which each parameter that is not a function parameter pack has a default argument (including the case of a constructor with no parameters).
(from the C++1z draft)
Older phrasing:
A default constructor for a class
X
is a constructor of classX
that can be called without an argument.
In addition, your copy constructor will be implicitly defined as defaulted, because you haven't declared one.
There is no such thing as a "default copy constructor". But "default constructor" and "defaulted copy constructor" are meaningful.
Default parameters in constructor gives compile time errors
Default values for parameters should go into the declaration, not the definition:
class C {
C(int a = 0);
}
C::C(int a) // not C::C(int a = 0)
{ ... }
Related Topics
Multiple Implicit Conversions on Custom Types Not Allowed
Critique My Non-Intrusive Heap Debugger
Why Do String Literals (Char*) in C++ Have to Be Constants
What Encoding Does Std::String.C_Str() Use
Maximum Stack Size for C/C+ Program
Is There Any Use for Local Function Declarations
Portable Zip Library for C/C++ (Not an Application)
How to Find the Current System Timezone
How to Read from a Text File, Character by Character in C++
Why Doesn't Delete Destroy Anything
Function to Mangle/Demangle Functions
Gcc - How to Create a Mapfile of the Object File
What Is the Meaning of Auto When Using C++ Trailing Return Type
How to Know If a Type Is a Specialization of Std::Vector