Compiler-Generated Default Functions

Discussion in 'C++' started by moleskyca1@yahoo.com, Jul 25, 2006.

  1. Guest

    Hi,

    In a recent discussion, some of us were in disagreement about the
    functions the C++ compiler generates. How many functions are generated
    by the compiler when you declare:

    class Foo
    {
    }; ?

    I thought 4. As in:

    class Foo
    {
    public:
    Foo(); // default constructor
    Foo(const Foo& f); // default copy constructor

    ~Foo(); // default destructor
    Foo &operator(const Foo& f); // default assigment operator
    };

    Is this list correct? Is that all?

    Someone said you must also add the new and delete operators, so that's
    6. I have not read the standard, but I'd argue that if this is so, then
    you should also count operator* () and operator& ()
    , Jul 25, 2006
    #1
    1. Advertising

  2. wrote:
    > In a recent discussion, some of us were in disagreement about the
    > functions the C++ compiler generates. How many functions are generated
    > by the compiler when you declare:
    >
    > class Foo
    > {
    > }; ?
    >
    > I thought 4. As in:
    >
    > class Foo
    > {
    > public:
    > Foo(); // default constructor
    > Foo(const Foo& f); // default copy constructor


    This is called "a copy constructor", not "default copy constructor"

    > ~Foo(); // default destructor


    There is no such thing as a "default destructor". There is always
    only one destructor.

    > Foo &operator(const Foo& f); // default assigment operator


    Probably just a typo. Should be:

    Foo &operator=(const Foo& f);


    And this is actually called "copy assignment operator".

    > };
    >
    > Is this list correct? Is that all?


    Yes. Yes.

    > Someone said you must also add the new and delete operators, so that's
    > 6. I have not read the standard, but I'd argue that if this is so,
    > then you should also count operator* () and operator& ()


    There is no "default" operator* for a class. If the class doesn't
    define its own 'new' and 'delete', the global ones are used. Also,
    there is no action you can take to make compiler omit those, so we
    usually don't count them. You *can* do something to make compiler not
    to generate a default c-tor, a copy-c-tor, or a copy-assignment-op.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 25, 2006
    #2
    1. Advertising

  3. wrote:
    > ...
    > In a recent discussion, some of us were in disagreement about the
    > functions the C++ compiler generates. How many functions are generated
    > by the compiler when you declare:
    >
    > class Foo
    > {
    > }; ?
    >
    > I thought 4. As in:
    >
    > class Foo
    > {
    > public:
    > Foo(); // default constructor
    > Foo(const Foo& f); // default copy constructor
    >
    > ~Foo(); // default destructor
    > Foo &operator(const Foo& f); // default assigment operator
    > };
    >
    > Is this list correct? Is that all?


    If by "compiler-generated" you mean "implicitly _declared_ by the compiler",
    then you are right - that is all. As was noted before, the usage of the term
    "default" in the comments is incorrect. The first one is indeed the default
    constructor, while the rest are not "default" in any way.

    The compiler will not _define_ these functions until you actually make an
    attempt to use them in your code.

    > Someone said you must also add the new and delete operators, so that's
    > 6. I have not read the standard, but I'd argue that if this is so, then
    > you should also count operator* () and operator& ()


    No. Nothing else should be there.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jul 25, 2006
    #3
  4. Uday Bidkar Guest

    wrote:

    > Hi,
    >
    > In a recent discussion, some of us were in disagreement about the
    > functions the C++ compiler generates. How many functions are generated
    > by the compiler when you declare:
    >
    > class Foo
    > {
    > }; ?
    >
    > I thought 4. As in:
    >
    > class Foo
    > {
    > public:
    > Foo(); // default constructor
    > Foo(const Foo& f); // default copy constructor
    >
    > ~Foo(); // default destructor
    > Foo &operator(const Foo& f); // default assigment operator
    > };
    >
    > Is this list correct? Is that all?
    >
    > Someone said you must also add the new and delete operators, so that's
    > 6. I have not read the standard, but I'd argue that if this is so, then
    > you should also count operator* () and operator& ()


    Hi,
    Going bit further, if you have a class hierachy lets say
    class Base
    {
    };
    class Dervd : public Base
    {
    };
    then compiler also provides a type conversion operator for class
    Dervd to allow
    conversion from Der1* to Base*

    Regards,
    Uday Bidkar
    Uday Bidkar, Jul 26, 2006
    #4
  5. Uday Bidkar wrote:
    > [..]
    > Going bit further, if you have a class hierachy lets say
    > class Base
    > {
    > };
    > class Dervd : public Base
    > {
    > };
    > then compiler also provides a type conversion operator for class
    > Dervd to allow
    > conversion from Der1* to Base*


    Yes, but *you* can't do that. That conversion is _standard_ and is
    always going to be there if the conditions are met (base class has to
    be accessible and non-ambiguous). Try providing a conversion from
    a pointer to A to a pointer to B if A and B are unrelated...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,591
    Victor Bazarov
    Jun 21, 2004
  2. Replies:
    22
    Views:
    818
    Frederick
    Jul 3, 2006
  3. JohnQ
    Replies:
    6
    Views:
    362
    JohnQ
    Feb 14, 2007
  4. JohnQ
    Replies:
    43
    Views:
    1,156
    James Kanze
    Jun 19, 2007
  5. Replies:
    1
    Views:
    327
Loading...

Share This Page