Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

A question about defaulted default constructors and throwing objects in C++

+2 votes
asked Dec 10, 2021 by Areeb Sherjil (1,960 points)
Recently I've been getting very confused by constructors especially the defaulted default constructor.

Consider this:

class Random

{

public:

/*some functions but no constructor defined*/

private:

/* some variables  */

};

Main questions:

1- If you do not define a constructor, is there a default constructor made that takes no arguments right? If so what is the purpose defining a constructor like this 'Random()=default; ' ?

2- When defining a single argument constructor why is it considered good practice to use 'explicit' keyword? I read somewhere that explicit keyword is to avoid implicit type conversion but I do not understand this, can someone give examples?

Sample code for explicit constructor when throwing objects:

https://onlinegdb.com/Nkia9xw36

This is a trivial code that throws an object and has a explicit constructor.

1 Answer

0 votes
answered Dec 10, 2021 by Peter Minarik (87,340 points)
selected Dec 10, 2021 by Areeb Sherjil
 
Best answer

Default Constructor

For details, please read about the default constructor here.

One idea of why you'd like to use the = default to declare your default constructor would be if you have (one or more) constructor(s) with parameters. In this case, the default (parameterless) constructor is not generated. If you still need it, you can ask the compiler to create one for you, unless you want to do it on your own. :)

Example

class Foo
{
public:
    Foo(int v) : _value(v) { }
    Foo() = default;

private:
    int _value;
};

int main()
{
    Foo f;
    return 0;
}

Explicit

I think explicit is explained pretty well here.

Basically, it's used to prevent automatic conversion from one object type to another. Only direct call to the constructor is allowed.

Example

A a1 = 42; // Only allowed if A::A(int) is not explicit
A a2(42);  // Always allowed
commented Dec 10, 2021 by Areeb Sherjil (1,960 points)
The default constructor makes sense now.
When you wrote A a1=42; what is happening here? As in what type of implicit conversion is happening and I thought you couldn't convert anything without overloading the '()' operator?
commented Dec 10, 2021 by Peter Minarik (87,340 points)
If you read the page I linked for you (https://en.cppreference.com/w/cpp/language/explicit, Notes section), you'd find the following: "A constructor with a single non-default parameter (until C++11) that is declared without the function specifier explicit is called a converting constructor."

So it's the converting constructor that's get called automatically (unless it's made explicit).

This has nothing to do with the overloading of the () -- function call -- operator (see here: https://en.cppreference.com/w/cpp/language/operators).

"A a2(42)" is a declaration and definition by calling the constructor: A(int)
commented Dec 10, 2021 by Areeb Sherjil (1,960 points)
ok makes sense now. I find the cpp reference page hard to follow at times. There's far too many templates there.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...