Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default vs. Implicit constructor in C++

This is very trivial, but Czech language (my native) doesn't distinguish between implicit and default, so I am confused by some Czech translations what is the difference between implicit and default constructor or constructor call.

struct Test {   Test(int n=0) { } }; 

Can you describe in these terms what is:

  1. Test t1;
  2. Test t2();
  3. Test t3 = 3;
  4. Test t4(4);
  5. Test t5 = Test(5);

?

like image 611
Jan Turoň Avatar asked Sep 09 '12 15:09

Jan Turoň


People also ask

Can a default constructor be called implicitly?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

What is the difference between default constructor and constructor?

A Default constructor is defined to have no arguments at all as opposed to a constructor in general which can have as many arguments as you wish.

What are the 3 types of constructor?

Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly 3 types of constructors in C++, Default, Parameterized and Copy constructors.

What is difference between default and user defined constructor?

The default constructor is a constructor that the compiler automatically generates in the absence of any programmer-defined constructors. Conversely, the parameterized constructor is a constructor that the programmer creates with one or more parameters to initialize the instance variables of a class.


2 Answers

The terms default and implicit, when talking about a constructor have the following meaning:

  • default constructor is a constructor that can be called with no arguments. It either takes no arguments or has default values for each of the arguments taken.

  • implicit constructor is a term commonly used to talk about two different concepts in the language, the

    • implicitly declared constructor which is a default or copy constructor that will be declared for all user classes if no user defined constructor is provided (default) or no copy constructor is provided (copy). That is, a class with no constructors declared by the user has one default constructor implicitly declared.

    • implicitly defined constructor is a implicitly declared constructor that is used (odr-used1 in the language and for which the compiler will provide a definition.

     

    struct test {     test(int i = 0) { }     // test(test const&) implicitly declared here };  struct test2 { };      // implicitly declared: test2(), test2(test2 const&)  int main() {     test t;      test copy(t);      // causes *definition* of the implicitly                        // declared copy constructor      test2 t2;          // causes *definition* of test2::test2()      test2 copy2(t2);   // causes *definition* of test2::test2(test2 const&) } 

In simple terms, a constructor is default if it can be called with no arguments. A constructor is implicit(ly declared/defined) if it is not provided by the user but declared/defined.

As of the specific cases:

Test t1; 

Uses the default constructor, Test(int = 0), which is not implicit.

Test t2(); 

This is a strange quirk of the language, it declares a function that takes no arguments and returns a Test object.

Test t3 = 3; 

This is called copy-initialization and is equivalent to the composition of an implicit* conversion from 3 to Test and copy construction of t3 from the result of the conversion. This will use the Test(int) constructor for the conversion, and then the implicitly defined (and declared) copy constructor. Note: the compiler can optimize away the copy, but it must verify that the copy constructor is available (access specifiers) and can be defined.

Test t4(4); 

Uses the Test(int) constructor, which in this case is not acting as a default constructor.

Test t5 = Test(5); 

Equivalent to the Test t3 = 3 case, with the only difference that the conversion from 5 to Test is explicit in this case. In this example it won't matter, but if the constructor had been marked as explicit this line would compile while the t3 case would fail.


*) Yet another use of implicit, in this case referring to the fact that the conversion from 3 to Test is not explicitly requested in the code. Compare this with t5 where the conversion is explicitly requested by the programmer: Test(5).

like image 148
David Rodríguez - dribeas Avatar answered Oct 09 '22 14:10

David Rodríguez - dribeas


You seem to be confusing some of the terms. A default constructor is one which takes no parameters, an implicit call is when you directly call the constructor.

Anyway:

1) Test t1;

Default constructor.

2) Test t2();

Function declaration.

3) Test t3 = 3;

Copy initialization. Will call the conversion constructor, create a temporary Test from 3, and use the copy constructor to create t3.

4) Test t4(4);

Direct initialization. Uses the conversion constructor directly.

5) Test t5 = Test(5);

Copy initialization. Similar to 3).

like image 24
Luchian Grigore Avatar answered Oct 09 '22 15:10

Luchian Grigore