conditions for automatic generation of default ctor, copy ctor,and default assignment operator (oper

Discussion in 'C++' started by puzzlecracker, Apr 14, 2008.

  1. From my understanding, if you declare any sort of constructors,
    (excluding copy ctor), the default will not be included by default. Is
    this correct?

    class Foo{
    public:
    Foo(int); // no Foo() is included, i believe.
    };



    Under which conditions a default copy ctor and default assignment
    operator are not included?


    Thanks.
    puzzlecracker, Apr 14, 2008
    #1
    1. Advertising

  2. puzzlecracker

    sk_usenet Guest

    Re: conditions for automatic generation of default ctor, copy ctor, and default assignment operator (operator)

    "puzzlecracker" <> wrote in message

    > From my understanding, if you declare any sort of constructors,
    > (excluding copy ctor), the default will not be included by default. Is
    > this correct?


    No, even declaring/defining an explicit copy c'tor would restrict compiler
    from generating a default constructor.

    > class Foo{
    > public:
    > Foo(int); // no Foo() is included, i believe.


    No default constructor would be generated in this case. Default assignment
    operator won't be affected.

    > };
    >
    >


    --
    http://techytalk.googlepages.com
    sk_usenet, Apr 14, 2008
    #2
    1. Advertising


  3. > No, even declaring/defining an explicit copy c'tor would restrict compiler
    > from generating a default constructor.


    What about the reverse - Will declaring an explicit default ctor,
    cause a class to generate a default copy constructor?


    How is assignment affected? Say we generate an overload version of
    assignment operator, will a default be included in the class?


    Thanks
    puzzlecracker, Apr 14, 2008
    #3
  4. puzzlecracker

    sk_usenet Guest

    Re: conditions for automatic generation of default ctor, copy ctor, and default assignment operator (operator)

    "puzzlecracker" <> wrote in message
    >
    >> No, even declaring/defining an explicit copy c'tor would restrict
    >> compiler
    >> from generating a default constructor.

    >
    > What about the reverse - Will declaring an explicit default ctor,
    > cause a class to generate a default copy constructor?


    Yes. Just from compilation perspectives it should be fine (Try it without
    defining it).

    > How is assignment affected? Say we generate an overload version of
    > assignment operator, will a default be included in the class?


    Yes
    --
    http://techytalk.googlepages.com
    sk_usenet, Apr 14, 2008
    #4
  5. puzzlecracker wrote:
    >> No, even declaring/defining an explicit copy c'tor would restrict compiler
    >> from generating a default constructor.

    >
    > What about the reverse - Will declaring an explicit default ctor,


    By "explicit" here you probably mean "user-declared". The term
    "explicit" has a certain specific meaning when applied to constructors
    in C++, you know...

    > cause a class to generate a default copy constructor?


    Again, a problem with terminology. There's no such thing as "default
    copy constructor". There's the default constructor. And there's the copy
    constructor. Both can be ether user-declared or compiler-declared.

    I assume your question is about the compiler-declared copy constructor.
    Providing a user-declared default constructor will not really "cause"
    anything to happen to compiler-declared copy constructor, i.e. it will
    not prevent the compiler from declaring the copy constructor in
    situations when you don't declare it yourself.

    > How is assignment affected? Say we generate an overload version of
    > assignment operator, will a default be included in the class?


    Yes. Regardless of what other assignment operators you might declare, if
    you don't declare the copy assignment operator, the compiler will
    declare one for you.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Apr 14, 2008
    #5
  6. puzzlecracker

    James Kanze Guest

    On Apr 14, 6:16 pm, puzzlecracker <> wrote:
    > From my understanding, if you declare any sort of
    > constructors, (excluding copy ctor), the default will not be
    > included by default. Is this correct?


    No. Any user defined constructor, including a copy constructor,
    inhibits automatic generation of the default constructor.

    > class Foo{
    > public:
    > Foo(int); // no Foo() is included, i believe.
    >
    > };


    > Under which conditions a default copy ctor and default
    > assignment operator are not included?


    A copy constructor is generated any time there is no user
    declared copy construtor. A copy assignment operator is
    generated any time there is no user declared copy assignment
    operator. A copy constructor for a class X is a non-template
    constructor having an X&, X const&, X volatile& or X const
    volatile& as its first parameter, and having default arguments
    for all other parameters, if any. A copy assignment operator is
    a non-template assignment operator having an X, X&, X const&, X
    volatile& or X const volatile& as parameter. Note that:

    -- a member function template template is never considered a
    copy constructor or copy assignment operator (and thus will
    never prevent the compiler from generating one);

    -- you can declare more than one copy constructor or copy
    assignment operator; and

    -- operator overload resolution is *always* used when deciding
    what constructor to call, even when "copying", and operator
    overload resolution can choose a constructor which is not a
    copy constructor to copy.

    Consider:

    class C
    {
    public:
    template< typename T >
    C( T& other ) ;
    } ;

    The compiler will generate a C::C( C const& ) copy constructor,
    which will participate in operator overload resolution:

    C c1 ;
    C c2( c1 ) ; // calls the template constructor.
    C c3( C() ) ; // calls the compiler generated copy
    // constructor.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #6
  7. puzzlecracker

    sasha Guest


    >
    > class C
    > {
    > public:
    > template< typename T >
    > C( T& other ) ;
    > } ;
    > C c1 ;
    > C c2( c1 ) ; // calls the template constructor.
    > C c3( C() ) ; // calls the compiler generated copy
    > // constructor.


    That's interesting, I didn't know template copy ctor that takes non-
    const, supersedes non-template ctor that take const type, when user
    passes const type
    sasha, Apr 15, 2008
    #7
  8. sasha wrote:
    >> class C
    >> {
    >> public:
    >> template< typename T >
    >> C( T& other ) ;
    >> } ;
    >> C c1 ;
    >> C c2( c1 ) ; // calls the template constructor.
    >> C c3( C() ) ; // calls the compiler generated copy
    >> // constructor.

    >
    > That's interesting, I didn't know template copy ctor that takes non-
    > const, supersedes non-template ctor that take const type, when user
    > passes const type


    Sorry, but what you said doesn't seem to make much sense.

    Firstly, there's no const type in this example.

    Secondly, when user does actually use an initializer of const type, the
    constructor that takes a non-const reference will not even qualify as a
    viable function. So no, it will not supersede anything. The behavior
    will be exactly opposite of what you said.

    How did you manage to derive these strange conclusions from the previous
    post from James is a mystery to me.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Apr 15, 2008
    #8
  9. puzzlecracker

    James Kanze Guest

    On 15 avr, 19:10, sasha <> wrote:
    > > class C
    > > {
    > > public:
    > > template< typename T >
    > > C( T& other ) ;
    > > } ;
    > > C c1 ;
    > > C c2( c1 ) ; // calls the template constructor.
    > > C c3( C() ) ; // calls the compiler generated copy
    > > // constructor.


    > That's interesting, I didn't know template copy ctor that
    > takes non- const, supersedes non-template ctor that take const
    > type, when user passes const type


    I don't understand your reasoning. There's not a single const
    object in my example. The template constructor is chosen when
    the argument is a non-const lvalue, because the instantiation of
    the template (C::C(C&)) is a better match. The compiler
    generated copy constructor has the signature C::C( C const& )
    (in this case), and will be called when the argument is const,
    or isn't an lvalue---both case in which the template constructor
    couldn't be called.

    Note that the compiler generated copy constructor doesn't always
    take a const reference---if the class contains a member or has a
    base whose copy constructor (user provided) takes a non-const
    reference, the the compiler generated copy constructor will also
    take a non-const reference. Such cases are (hopefully) rare,
    however.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #9
    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. Apricot
    Replies:
    4
    Views:
    524
    velthuijsen
    Apr 16, 2004
  2. vineoff

    friend oper<<

    vineoff, Jan 9, 2006, in forum: C++
    Replies:
    9
    Views:
    318
    Thomas Tutone
    Jan 9, 2006
  3. Mike - EMAIL IGNORED

    base class copy ctor and assignment operator

    Mike - EMAIL IGNORED, Mar 21, 2006, in forum: C++
    Replies:
    1
    Views:
    326
    Alf P. Steinbach
    Mar 21, 2006
  4. NVH
    Replies:
    8
    Views:
    485
    mlimber
    Jul 6, 2006
  5. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    407
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
Loading...

Share This Page