0

Well I understand the part that I will be getting some random value, but is theFoo() constructor in the snippet acting just like the default public constructor which the compiler supplies when we have no constructor defined?

#include<iostream>
using namespace std ;

class Foo{
        int i  ;
    public:
        Foo(){
        }
        void disp(){
            cout<<"i = "<<i  ;
        }
};

int main(){
    Foo bar1,bar2 ;
    bar1.disp();
    cout<<"\n";
    bar2.disp();
}

I have seen some people writing an empty constructor like this, but I could't understand exactly why/when is it to be used?

Tasdik Rahman
  • 2,160
  • 1
  • 25
  • 37
  • 2
    http://stackoverflow.com/questions/4482113/why-would-someone-provide-an-empty-default-constructor-for-a-class – Romo Daneghyan Sep 16 '14 at 15:55
  • This is actually quite complicated. The constructor itself does the same as the compiler provided one would do. But the fact that there is a user provided constructor changes the behaviour, particularly under [*value initialiation*](http://en.cppreference.com/w/cpp/language/value_initialization). To confuse matters further, the details change between C++03, C++11 and C++14. – juanchopanza Sep 16 '14 at 16:10
  • Did they forget to initialize `i` in it? – Neil Kirk Sep 16 '14 at 16:15

3 Answers3

2

A user-defined ctor without arguments, without ctor-init-list and with an empty body behaves nearly the same as the default-ctor.
There is one difference though, it does not count as a trivial ctor, ever!

Explicitly defaulting like this instead would avoid that difference and the concomittant potential performance-degradation:

Foo() = default; // Needs C++11

What does "default" mean after a class' function declaration?

See also <type_traits> for the easy way to detect the difference: http://en.cppreference.com/w/cpp/types/is_constructible

Community
  • 1
  • 1
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
1

When you provide the definition of an empty constructor, compiler does not provide the default constructor and initialize its own way to the members. You are just not allowing compiler to do its default initializations.

Subhajit
  • 320
  • 1
  • 6
0

An empty constructor is required for each class. If you want to have a constructor with initialization logic, you can add it along with the empty constructor. In some languages, if you do not write an empty constructor explicitly, compiler will generate it for you.

If is just to create an instance of a class and it does nothing else.

You can overload it with parameters to have another constructor that initializes its properties.

gurrawar
  • 363
  • 2
  • 8
  • 17
  • 1
    But an empty constructor is not required for each class. And this doesn't answer the question. – juanchopanza Sep 16 '14 at 16:00
  • @juanchopanza, empty constructor is required, if you do not provide one, compiler will generate for you. read my response again and it answers the question. This is C# not C++ :) – gurrawar Feb 26 '15 at 15:43