Difference between default copy constructor vs no copy constructor [duplicate] - c++

This question already has answers here:
Defaulted constructor vs implicit constructor
(2 answers)
What is the difference between constructor "=default" and the compiler generated constructor in C++?
(3 answers)
Closed 4 months ago.
In C++, is there a difference between declaring a copy constructor default as opposed to not declaring one at all? For visualization:
class A{
int x;
}
vs
class B{
int x;
B() = default;
B(const B&) = default;
}
How about default copy assignment, default move constructor, and default move assignment? If I'm just going to declare them as default, could I just as well not declare them at all?
I looked on the internet and haven't been able to find an answer

Related

Do I have to define a default constructor for my class if I want to use "new" to allocate an array of it? [duplicate]

This question already has answers here:
Object array initialization without default constructor
(14 answers)
Closed 2 years ago.
Let's say I have a class A :
class A{
public:
A(int const& someValue, std::string const& anotherOne);
private:
std::string m_string_member;
int m_int_member;
};
And I want to allocate an array of this class using new :
A* myClassArray = new A[69];
I get the error : No default constructor available.
Do I have to write a default constructor for every class I want to use by calling new ?
Short answer: Yes.
Long answer: Lets start with this matrix here https://foonathan.net/images/special-member-functions.png. You provide a constructor which is not the default constructor (yours takes arguments). So the compiler won't automagically generate a default constructor for you. However, in your allocation, you ask the compiler to default construct 69 elements. How should the compiler do this? But, solving this issue is rather easy. Just provide the default constructor, or, even easier, use = default. The latter only works because all your members are default-constructible.
In c++ if no user-declared constructors of any kind are provided for a class type the compiler will always declare a default constructor. If you provide any constructor and want default constructor as well, you should define default constructor explicitly.

In c++, if I create a constructor that takes one argument which has a default value - will that serve as a default (empty) constructor? [duplicate]

This question already has an answer here:
In C++, is a constructor with only default arguments a default constructor?
(1 answer)
Closed 4 years ago.
Does the following one-parameter constructor also serve as a default constructor?
class SomeClass
{
public:
SomeClass(const int &a = 4);
}
(Assuming the constructor is well defined etc.)
Thanks!
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 class X 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 class X 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.

Class default constructor [duplicate]

This question already has answers here:
Two constructors, which is default?
(6 answers)
Closed 8 years ago.
Assuming that we have the class TestClass in our C++ project. A default constructor is the one empty parameters list. So we have:
TestClass();
TestClass(int defaultParam = 0);
Can these two be considered default constructors? And if they can be, is it ethical to have a default constructor like the second line?
Either of
TestClass(void);
TestClass(int defaultParam=0);
can be used as the default constructor. When you have both, it is a problem since the compiler cannot distinguish between the two when the compiler needs to use a default constructor. E.g.
TestClass anObject;
TestClass objectArray[5];
Unrelated to your question
For stylistic reasons, you should use:
TestClass();
instead of
TestClass(void);
The second form is supported by C++ but it's not necessary. The argument type void is necessary only when declaring functions in C.
having more than 1 constructor is called constructor overloading. If there are two default constructors it will generate an error as the compiler will not know which constructor to call while creating the object. If you don't declare a default constructor the complier does it by itself.

User-declared default constructor + in-class initializers != user-provided constructor? [duplicate]

This question already has answers here:
Why does C++ require a user-provided default constructor to default-construct a const object?
(5 answers)
Closed 5 years ago.
The Clang documentation neatly explains that
If a class or struct has no user-defined default constructor, C++
doesn't allow you to default construct a const instance of it like
this ([dcl.init], p9)
The rationale being that if a const object is not correctly initialized, it cannot be changed later on. The following code has only a user-declared default constructor for Test, but all its members have in-class initializers,
#include<iostream>
class Test
{
public:
Test() = default;
void print() const { std::cout << i << "\n"; }
private:
int i = 42; // will propagate to the default constructor!
};
int main()
{
Test const t; // <-- Clang chokes on the const keyword, g++ does not
t.print(); // prints 42
}
so the rationale for also user-providing the default constructor seems superfluous to me. And indeed, g++ 4.8.1 does compile it without problems (Online Example), although Clang <= 3.2 does not.
Questions: why is the combination of complete in-class initalizers + user-declared default constructor not enough to default construct a const object? Is there a fix underway for the C++14 Standard?
UPDATE: can anyone try on Clang 3.3 / 3.4 to see if this has been fixed compared to Clang 3.2?
Yes, this is a known problem. See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#253 . It hasn't been fixed yet in the spec.

c++ empty constructor and member initialization [duplicate]

This question already has answers here:
Does a constructor / destructor have to have code, or is the function enough?
(3 answers)
Closed 9 years ago.
I have confusion regarding default and empty constructor. Does empty constructor also initializes class variable automatically ? Meaning if i use a empty constructor instead of default constructor , does that also initialize class member variable automatically ? For example, if use following code, does integer pointer is initialized to NULL ? Please confirm
// .h file
Class Test {
public:
Test();
~Test();
int *p;
}
// .cpp file
Test::Test()
{
// do something..
}
No, empty constructor is same as default constructor if you don't initialize any member variable inside it.