Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:39:53+00:00 2026-05-31T01:39:53+00:00

If the class doesn’t have the constructor, will the compiler make one default constructor

  • 0

If the class doesn’t have the constructor, will the compiler make one default constructor for it ?

Programmers new to C++ often have two common misunderstandings:

That a default constructor is synthesized for every class that does
not define one

from the book Inside the C++ Object Model

I am at a loss…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:39:54+00:00Added an answer on May 31, 2026 at 1:39 am

    This is well explained in the section from which this quote is taken. I will not paraphrase it in its entirety, but here is a short summary of the section content.

    First of all, you need to understand the following terms: implicitly-declared, implicitly-defined, trivial, non-trivial and synthesized (a term that is used by Stanley Lippman, but is not used in the standard).

    implicitly-declared

    A constructor is implicitly-declared for a class if there is no user-declared constructor in this class. For example, this class struct T { }; does not declare any constructor, so the compiler implicitly declares a default constructor. On the other hand, this class struct T { T(int); }; declares a constructor, so the compiler will not declare an implicit default constructor. You will not be able to create an instance of T without parameters, unless you define your own default constructor.

    implicitly-defined

    An implicitly-declared constructor is implicitly-defined when it is used, i.e. when an instance is created without parameters. Assuming the following class struct T { };, the line T t; will trigger the definition of T::T(). Otherwise, you would have a linker error since the constructor would be declared but not defined. However, an implicitly-defined constructor does not necessarily have any code associated with it! A default constructor is synthesized (meaning that some code is created for it) by the compiler only under certain circumstances.

    trivial constructor

    An implicitly-declared default constructor is trivial when:

    • its class has no virtual functions and no virtual base classes and
    • its base classes have trivial constructors and
    • all its non-static members have trivial constructors.

    In this case, the default compiler has nothing to do, so there is no code synthesized for it. For instance, in the following code

    struct Trivial
    {
        int i;
        char * pc;
    };
    
    int main()
    {
        Trivial t;
    }
    

    the construction of t does not involve any operations (you can see that by looking at the generated assembly: no constructor is called to construct t).

    non-trivial

    On the other hand, if the class does not meet the three requirements stated above, its implicitly-declared default constructor will be non-trivial, meaning that it will involve some operations that must be performed in order to respect the language semantics. In this case, the compiler will synthesize an implementation of the constructor performing these operations.

    For instance, consider the following class:

    struct NonTrivial
    {
        virtual void foo();
    };
    

    Since it has a virtual member function, its default constructor must set the virtual table pointer to the correct value (assuming the implementation use a virtual method table, of course).

    Similarly, the constructor of this class

    struct NonTrivial
    {
        std::string s;
    };
    

    must call the string default constructor, as it is not trivial. To perform these operations, the compiler generates the code for the default constructor, and calls it anytime you create an instance without parameters. You can check this by looking at the assembly corresponding to this instantiation NonTrivial n; (you should see a function call, unless the constructor has been inlined).


    Summary

    When you don’t provide any constructor for your class, the compiler implicitly declares a default one. If you try to use it, the compiler implicitly defines it, if it can (it is not always possible, for instance when a class has a non-default-constructible member). However, this implicit definition does not imply the generation of any code. The compiler needs to generate code for the constructor (synthesize it) only if it is non-trivial, meaning that it involves certain operations needed to implement the language semantics.


    N.B.

    Stanley B Lippman’s “Inside the C++ object model” and this answer deals with (a possible) implementation of C++, not its semantics. As a consequence, none of the above can be generalized to all compilers: as far as I know, an implementation is perfectly allowed to generate code even for a trivial constructor. From the C++ user point of view, all that matters is the “implicitly-declared/defined` aspect (and also the trivial/non-trivial distinction, as it has some implications (for instance, an object of a class with non-trivial constructor cannot be a member of a union)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

QGeoRoutingManager: http://apidocs.meego.com/1.0/qtmobility/qgeoroutingmanager-members.html This class doesn't have a constructor. I have forgotten the way to
I have a class with an object as a member which doesn't have a
Doesn't object initialization outside of a constructor break encapsulation ? Given: class MyClass {
The DateTimeOffset property I have in this class doesn't get rendered when the data
I have a base class called SpriteSheet which has two children, RotatedSpriteSheet and FlippedSpriteSheet.
The Media class doesn't have function which allows the needed functionality. Does a better
From what I have gathered from internets the MPMoviePlayerController class doesn't support small video
Well for Pictures we have QDeclarativeImageProvider but seems that the class doesn't support animated
I have a Base Class with two constructors, requiring a parameter: public abstract class
Why Child class doesn't have echo() method? Parent = function(){ this.name = 'abc'; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.