Default constructor construction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Satish Sangapu

    Default constructor construction

    I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a very
    nice book about the internals of C++.
    Anyway, I am confused about one thing. He states that a default constructor
    is not ALWAYS generated by the compiler - ONLY when it's NEEDED and then he
    goes on to define NEEDED.

    class one
    {
    private:
    int a;
    bool b;
    };

    Apparently, the above class won't have a default constructor generated by
    the compiler.

    But,

    class data
    {
    public:
    data();
    };

    class two
    {
    private:
    data d;
    };

    The above two class will have a default constructor generated by the
    compiler.

    The book was written in 1996 - I wonder if stuff like this has changed by
    now?

    Thanks.


  • Victor Bazarov

    #2
    Re: Default constructor construction

    "Satish Sangapu" <sksjava@hotmai l.com> wrote...[color=blue]
    > I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a[/color]
    very[color=blue]
    > nice book about the internals of C++.
    > Anyway, I am confused about one thing. He states that a default[/color]
    constructor[color=blue]
    > is not ALWAYS generated by the compiler - ONLY when it's NEEDED and then[/color]
    he[color=blue]
    > goes on to define NEEDED.
    >
    > class one
    > {
    > private:
    > int a;
    > bool b;
    > };
    >
    > Apparently, the above class won't have a default constructor generated by
    > the compiler.
    >
    > But,
    >
    > class data
    > {
    > public:
    > data();
    > };
    >
    > class two
    > {
    > private:
    > data d;
    > };
    >
    > The above two class will have a default constructor generated by the
    > compiler.
    >
    > The book was written in 1996 - I wonder if stuff like this has changed by
    > now?[/color]

    The default constructor by the definition can be _trivial_ or
    _non-trivial_. In case of "class one", it's _trivial_. For
    the "class two", the only member has non-trivial constructor,
    which makes "class two"s constructor also non-trivial.

    See section 12.1 of the Standard.

    Victor



    Comment

    • Icosahedron

      #3
      Re: Default constructor construction

      "Satish Sangapu" <sksjava@hotmai l.com> wrote in message news:<bpvp88$q3 s$1@news.lsil.c om>...[color=blue]
      > I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a very
      > nice book about the internals of C++.
      > Anyway, I am confused about one thing. He states that a default constructor
      > is not ALWAYS generated by the compiler - ONLY when it's NEEDED and then he
      > goes on to define NEEDED.
      >
      > class one
      > {
      > private:
      > int a;
      > bool b;
      > };
      >
      > Apparently, the above class won't have a default constructor generated by
      > the compiler.
      >
      > But,
      >
      > class data
      > {
      > public:
      > data();
      > };
      >
      > class two
      > {
      > private:
      > data d;
      > };
      >
      > The above two class will have a default constructor generated by the
      > compiler.
      >
      > The book was written in 1996 - I wonder if stuff like this has changed by[/color]
      [color=blue]
      > now?
      >
      > Thanks.[/color]

      While perhaps it has changed, I doubt it would still generate a
      default constructor. The first class has no user defined types, so it
      doesn't need a constructor. If you declare an int or float or
      something else on the stack in a function, and don't initialize it,
      you get garbage because there is no constructor for POD
      (plain-old-data) types.

      class two, because it has a member of class data, which has a
      constructor, generates a constructor to do one thing, call the
      constructor of data.

      HTH,

      Jay

      Comment

      • Ron Natalie

        #4
        Re: Default constructor construction


        "Icosahedro n" <gandalf_istari @hotmail.com> wrote in message news:9119d99.03 11251124.5368cf a@posting.googl e.com...
        [color=blue]
        > you get garbage because there is no constructor for POD
        > (plain-old-data) types.[/color]

        Any class that doesn't provide a declaration of a constructor, gets a
        default constructor impliictly declared by the compiler. POD's are not an exception to this
        rule (as a matter of fact, the rule primarily exists to support POD's).

        A constructor is defined whenever it is needed, that is, when someone
        actually tries to build a default-constructed object. Most compilers will
        optimize away the call of a trivial cosntructor, but that doesn't change
        it's "existance" as far as the language is concerned.


        Comment

        • E. Robert Tisdale

          #5
          Re: Default constructor construction

          Satish Sangapu wrote:
          [color=blue]
          > I am reading "Inside the C++ Object Model" by Stanley Lippman.
          > It is a very nice book about the internals of C++.
          > Anyway, I am confused about one thing. He states that
          > a default constructor is not ALWAYS generated by the compiler -
          > ONLY when it's NEEDED and then he goes on to define NEEDED.
          >
          > class one {
          > private:
          > int a;
          > bool b;
          > };
          >
          > Apparently, the above class won't have
          > a default constructor generated by the compiler.[/color]

          Wrong.
          [color=blue]
          > But,
          >
          > class data {
          > public:
          > data(void);
          > };
          >
          > class two {
          > private:
          > data d;
          > };
          >
          > The above two class will have
          > a default constructor generated by the compiler.[/color]

          Wrong.
          The default constructor for class data
          *must* be provided by you -- the programmer.
          [color=blue]
          > The book was written in 1996.
          > I wonder if stuff like this has changed by now?[/color]

          Nothing has changed in this regard.
          C++ compilers will "provide" a default constructor,
          a copy constructor and an assignment operator
          if you don't declare them.

          You need to be careful about what you mean by
          "generated by the compiler".
          C++ compilers don't need to write a default constructor.
          All they really need to do is emit code to construct the object.
          There may be no code that you can identify as
          "a call to the default constructor". It might be "as if"
          you defined all of your constructors inline
          and all of the code was "optimized away".
          The notion that the compiler "provided" or "generated"
          a default constructor may be purely conceptual.

          Comment

          • Andrey Tarasevich

            #6
            Re: Default constructor construction

            Satish Sangapu wrote:[color=blue]
            > I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a very
            > nice book about the internals of C++.
            > Anyway, I am confused about one thing. He states that a default constructor
            > is not ALWAYS generated by the compiler - ONLY when it's NEEDED and then he
            > goes on to define NEEDED.[/color]

            "Generated" is not a precise term in this context. Perhaps this term was
            explained in the book, but since you didn't provide the explanation here
            there is no way to know what it means. The C++ specification uses terms
            such terms as "declare" and "define" when it comes to providing implicit
            constructors.
            [color=blue]
            > class one
            > {
            > private:
            > int a;
            > bool b;
            > };
            >
            > Apparently, the above class won't have a default constructor generated by
            > the compiler.[/color]

            This class will have a default constructor implicitly _declared_ by the
            compiler. However, the compiler will not try to _define_ it unless it is
            really necessary (i.e. unless it is used in the program).
            [color=blue]
            > But,
            >
            > class data
            > {
            > public:
            > data();
            > };
            >
            > class two
            > {
            > private:
            > data d;
            > };
            >
            > The above two class will have a default constructor generated by the
            > compiler.[/color]

            No. Since you declared default constructor for class 'data' explicitly,
            the compiler will not define it for you. You have to do it yourself.

            As for the class 'two', the situation here is the same as with class
            'one' - the compiler will declare the constructior and will make an
            attempt to define it, if it is needed in the program.
            [color=blue]
            > The book was written in 1996 - I wonder if stuff like this has changed by
            > now?[/color]


            --
            Best regards,
            Andrey Tarasevich

            Comment

            Working...