Default Copy Constructor

Discussion in 'C++' started by Bob, Jun 12, 2004.

  1. Bob

    Bob Guest

    Hi,

    If a class contains only built-in types and standard library objects
    such as strings and vectors, is it safe to let the compiler create the
    copy constructor and assignment operator, or is it advised to always
    write your own?

    I know you need to write your own if there is any dynamic memory
    allocation etc, but I was curious as to the "correct" way in the case
    of static variables.

    Thanks,
    Bob.
     
    Bob, Jun 12, 2004
    #1
    1. Advertising

  2. Bob

    John Carson Guest

    "Bob" <> wrote in message
    news:
    > Hi,
    >
    > If a class contains only built-in types and standard library objects
    > such as strings and vectors, is it safe to let the compiler create the
    > copy constructor and assignment operator


    Yes.

    > or is it advised to always
    > write your own?


    No. In fact it is safer to let the compiler do it since you might make a
    mistake.

    There are occasions when you might need to create your own versions (e.g.,
    if you want to keep count of the number of objects created of a particular
    class) but, unless you want to do something beyond mere construction or
    assignment, the compiler-generated version is fine.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Jun 12, 2004
    #2
    1. Advertising

  3. Bob wrote in news: in
    comp.lang.c++:

    > Hi,
    >
    > If a class contains only built-in types and standard library objects
    > such as strings and vectors, is it safe to let the compiler create the
    > copy constructor and assignment operator, or is it advised to always
    > write your own?
    >


    The rule is quite simple:

    If all members have constructors, assigment operator and destructor that
    "do the right thing(tm)" then the compiler generated ctor's etc
    will do the right thing.

    Typically pointers that point to dynamic memory don't obey this rule.
    Another example would be the FILE * returned by fopen (it needs to
    be closed by fclose).

    Most of the types defined by the C++ part of the standard library
    do obey this rule, however ther are some oddity's:

    - std::auto_ptr< T > - it has strange copying behaviour (it moves).
    - std::eek:stream et al - doesen't provide for copying (ctor or op = ).

    > I know you need to write your own if there is any dynamic memory
    > allocation etc, but I was curious as to the "correct" way in the case
    > of static variables.
    >


    static variables aren't part of an instance, they're globals (but globals
    that are restricted to the classes scope) and don't need to be constructed
    by the copy constructor or assigned to by the assignment operator.


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jun 12, 2004
    #3
  4. "John Carson" <> wrote...
    > "Bob" <> wrote in message
    > news:
    > > Hi,
    > >
    > > If a class contains only built-in types and standard library objects
    > > such as strings and vectors, is it safe to let the compiler create the
    > > copy constructor and assignment operator

    >
    > Yes.


    Well, to be technically correct, _pointers_ _are_ in fact built-in types,
    but somehow I think the OP didn't mean pointers. In any case, mentioning
    "The Rule of Three" could also help.

    V
     
    Victor Bazarov, Jun 12, 2004
    #4
    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. Aire
    Replies:
    3
    Views:
    474
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    979
    Alf P. Steinbach
    Mar 6, 2006
  3. ali
    Replies:
    4
    Views:
    586
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,281
  5. cinsk
    Replies:
    35
    Views:
    2,651
    James Kanze
    Oct 11, 2010
Loading...

Share This Page