Is there an implicit default constructor in C++?

In the book I'm reading at the moment (C++ Without Fear) it says that if you don't declare a default constructor for a class, the compiler supplies one for you, which "zeroes out each data member". I've experimented with this, and I'm not seeing any zeroing -out behaviour. I also can't find anything that mentions this on Google. Is this just an error or a quirk of a specific compiler?

Is there an implicit default constructor in C++?, In computer programming languages, the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a nullary constructor. In other languages (e.g. in C++) it is a constructor that can be called without In a sense, this is an explicit call to the� The implicitly-declared or defaulted default constructor for class T is undefined (until C++11) defined as deleted (since C++11) if any of the following is true: T has a member of reference type without a default initializer. (since C++11) T has a const member without user-defined default constructor or a default member initializer (since C++11).

I think it's worth pointing out that the default constructor will only be created by the compiler if you provide no constructor whatsoever. That means if you only provide one constructor that takes an argument, the compiler will not create the default no-arg constructor for you.

The zeroing-out behavior that your book talks about is probably specific to a particular compiler. I've always assumed that it can vary and that you should explicitly initialize any data members.

Default constructor, C++ implicitly generates a default copy constructor which will call the copy constructors for all base classes and all member variables unless� No default constructor is created for a classthat has any constant or reference type members. A constructor of a class Ais trivialifall the following are true: It is implicitly declared or explicitly defaulted. Ahas no virtual functions and no virtual baseclasses.

Constructor (object-oriented programming), If no constructors are declared in a class, the compiler provides an implicit inline default constructor. C++. Copy. #include� A constructor without any parameter is called Default Constructor. If we do not define it explicitly, then it will be implicitly provided by the compiler. In such a case, we can call it implicit constructor.

C++ does generate a default constructor but only if you don't provide one of your own. The standard says nothing about zeroing out data members. By default when you first construct any object, they're undefined.

This might be confusing because most of the C++ primitive types DO have default "constructors" that init them to zero (int(), bool(), double(), long(), etc.), but the compiler doesn't call them to init POD members like it does for object members.

It's worth noting that the STL does use these constructors to default-construct the contents of containers that hold primitive types. You can take a look at this question for more details on how things in STL containers get inited.

Constructors (C++), Is the default constructor for Fred always Fred::Fred() ? might be the right thing to do, and for those, you might need the “explicit initialization of arrays” syntax. The two main types of constructors are default constructors and parameterized constructors. Default constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are 0.

The default constructor created for a class will not initialize built-in types, but it will call the default constructor on all user-defined members:

class Foo
     int x;
     Foo() : x(1) {}

class Bar
     int y;
     Foo f;
     Foo *fp;

int main()

    Bar b1; 
    ASSERT(b1.f.x == 1); 
    // We know nothing about what b1.y is set to, or what b1.fp is set to.

    // The class members' initialization parallels normal stack initialization.
    int y;  
    Foo f; 
    Foo *fp; 
    ASSERT(f.x == 1);
    // We know nothing about what y is set to, or what fp is set to.


Constructors, C++ FAQ, Default constructors (C++ only) � It is implicitly defined or explicitly defaulted � A has no virtual functions and no virtual base classes � All the direct base classes of A� As was previously mentioned, some types of serialization require a default constructor. There are also times when a class has multiple parameterized constructors but also requires "lower level" initialization, in which case a private default constructor can be used which is chained in from the parameterized constructors.

Default constructors (C++ only), The compiler will implicitly declare default constructor if not provided by programmer, will define it when in need. Compiler defined default� Whenever we define one or more non-default constructors( with parameters ) for a class, a default constructor( without parameters ) should also be explicitly defined as the compiler will not provide a default constructor in this case. However, it is not necessary but it’s considered to be the best practice to always define a default constructor.

C++ Internals | Default Constructors, A default constructor is one that can be called with no parameters, but there are two possible meanings of implicit constructor. The first is implicit conversion� §12.1/6 [class.ctor] A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used to create an object of its class type or when it is explicitly defaulted after its first declaration.

In C++, what is the difference between an implicit constructor and , If all the members of the class have a default constructor, the compiler will implicitly generate one for your class. C.44: Prefer default constructors� Implicit constructor is that which is provided by default in a class and explicit constructor is that which we specify explicitly in our class. If we provide constructor explicitly then there is no implicit constructor provided by the compiler and if there is no explicit constructor then compiler will provide implicit constructor. 221 views

  • Sounds like you need a better book. ;)
  • Yup. Unforgiveable error, if the "zeroes each data mamber" is indeed a literal quote.
  • I think this is a common c misconception. I thought the same thing until I visited this page. I'm sure I read it somewhere or I learned in a class.
  • I suspect this confusion arises when people learn that global objects are guaranteed to be zeroed, although it's really best not to rely on that either.
  • I should make a minor point that data is always zeroed out in a program. The operating system does this upon page allocation. The issue is all of the extra 'junk' C++ does before even running your program.
  • From section 8.5 item 5 in the standard: "To default-initialize an object of type T means: ... [if the T is POD] the object is zero-initialized." and from item 4 of the same section: "To zero-initialize an object of type T means: if T is a scalar type, the object is set to the value 0 (zero), taken as an integral constant expression, converted to T" . For POD, if I am not mistaken, this means int i = int(); concludes with i == 0
  • @wilhelmtell: That is when you explicitly use the default initialized. In the above situation the POD types are not explicitly default initialized so there values are undefined. This is why I use the slightly more ambiguous term of default constructor.
  • This is by far the best answer - unfortunately, I can upvote it only once. So everybody else should upvote, too!
  • Related -- value-initialization for structs:
  • The value used in debug mode is compiler dependant. And different values usually represent different states. Like Allocated/Deallocated/Not Initialized etc.
  • To clarify, those tagging values are usually set by the memory management library (operator new(), malloc, and derivatives), not by the compiler creating default constructors.
  • It's part of the standard though. Do you know a complaint compiler that doesn't zero out globals?