C++ constructors with default arguments [duplicate] - c++

This question already has answers here:
So can a class have two default constructors?
(4 answers)
Multiple Default Constructors
(2 answers)
Class X has more than one default constructor & ambiguous call to overloaded function
(1 answer)
Closed 9 days ago.
I know we can have both default constructor without arguments and all arguments with default values constructor at the same time, but what time we need to have default constructor with default values, can we use default constructor with default values instead of default constructor without default values?

Related

How can I find out why the compiler is automatically creating copy constructors in VS2012? [duplicate]

This question already has answers here:
Why disable CObject's copy constructor and assignment
(2 answers)
What are all the member-functions created by compiler for a class? Does that happen all the time?
(5 answers)
Closed 7 years ago.
I'm updating a VS2010 C++ application to VS2012 and I'm getting the error
cannot access private member declared in class 'CObject'
This appears to be due to the complier generated function
CObArray::CObArray(const CObArray &).
Is there a way I can disable the compile generated functions? Or a way of knowing why it's creating the functions?

Why do constructors need to be named exactly after the class? [duplicate]

This question already has answers here:
Why constructors will always have same name as of class and how they are invoked implicitly?
(7 answers)
Closed 8 years ago.
What is the reason a constructor needs to have the exact same name than the class? Is it purely a sintactic reason? Is it possible to define the constructor with a different name?
It needs to have the same name as the class to tell it apart from other (non-special) member functions. This is a convention for the constructor (and destructor, when prefixed with a tilde) to unambiguously tell the compiler that you are creating a constructor for your class and not just a member function in the class.

If I put an argument in my default constructor, but give the argument a default value, is it still really a default constructor? [duplicate]

This question already has an answer here:
In C++, is a constructor with only default arguments a default constructor?
(1 answer)
Closed 9 years ago.
I know it's said to be a default constructor, but how does it work behind the scenes? I'm getting a "procedure entry point could not be located" error when my program tries to use my library containing class A with this default constructor. The program doesn't even use the default constructor of class A; it uses other constructors of the A. The library builds fine; the program builds fine. The DLL has been rebuilt, so it should know its own method call when it sees it. I'm actually just completely lost.
class DLLEXPORT A
{
A(int a = 0); //default constructor and single parameter constructor
};
A default constructor is any constructor that is callable with no arguments.
When you say A x;, then this is the same as A x(0);, which is what the compiler actually calls.
Similarly, copy constructors can have additional, defaulted arguments.

Not using parentheses in constructor call with new (c++) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Do the parentheses after the type name make a difference with new?
So I had in my main:
Class* pC = new Class;
It was working as
Class* pC = new Class();
I realized just today that I had omitted the parentheses (so I was hit by the "opposite" of the most vexing parse in a way).
My question: Are these two forms equivalent ?
If the class has a default constructor defined, then both are equivalent; the object will be created by calling that constructor.
If the class only has an implicit default constructor, then there is a difference. The first will leave any members of POD type uninitialised; the second will value-initialise them (i.e. set them to zero).

explicit keyword [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does the explicit keyword in C++ mean?
what does the keyword explicit mean?
C++ constructors that have just one parameter automatically perform implicit type conversion. For example, if you pass an int when the constructor expects a string pointer parameter, the compiler will add the code it must have to convert the int to a string pointer. However, you might not always want this automatic behavior.
You can add explicit to the constructor declaration to prevent implicit conversions. This forces the code to either use a parameter of the correct type, or cast the parameter to the correct type. That is, if the cast is not visibly expressed in code, an error will result.
explicit (C++)