copy ctor vs default ctor

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Aug 15, 2007.

  1. , India

    , India Guest

    If we do not provide any ctor for a class, the compiler provides the
    default ctor and copy ctor if needed.

    Consider a class Test.
    Suppose we provide some ctor in class Test but do not provide the
    default ctor.

    Suppose we try to create

    Test obj;

    Here the default ctor is needed and the compiler does not provide the
    default ctor but generates a compilation error.

    However if we provide some ctor but do not provide the copy ctor, the
    compiler does not generate error but provides the copy ctor if needed.

    Why doesn't the compiler give error for the absence of copy ctor
    similar to error for the absence of default ctor.

    Kindly explain

    Thanks
    V.Subramanian
    , India, Aug 15, 2007
    #1
    1. Advertising

  2. , India wrote:
    > If we do not provide any ctor for a class, the compiler provides the
    > default ctor and copy ctor if needed.
    >
    > Consider a class Test.
    > Suppose we provide some ctor in class Test but do not provide the
    > default ctor.
    >
    > Suppose we try to create
    >
    > Test obj;
    >
    > Here the default ctor is needed and the compiler does not provide the
    > default ctor but generates a compilation error.
    >
    > However if we provide some ctor but do not provide the copy ctor, the
    > compiler does not generate error but provides the copy ctor if needed.
    >
    > Why doesn't the compiler give error for the absence of copy ctor
    > similar to error for the absence of default ctor.


    The C++ standard specifies it that way. I can only imagine that the C++
    standard was specified this way because it's more likely that if a
    default CTOR is not provided while others are, you more than likely
    don't want a default ctor while you do want a copy ctor.
    Gianni Mariani, Aug 15, 2007
    #2
    1. Advertising

  3. On 2007-08-15 08:55, , India wrote:
    > If we do not provide any ctor for a class, the compiler provides the
    > default ctor and copy ctor if needed.
    >
    > Consider a class Test.
    > Suppose we provide some ctor in class Test but do not provide the
    > default ctor.
    >
    > Suppose we try to create
    >
    > Test obj;
    >
    > Here the default ctor is needed and the compiler does not provide the
    > default ctor but generates a compilation error.
    >
    > However if we provide some ctor but do not provide the copy ctor, the
    > compiler does not generate error but provides the copy ctor if needed.
    >
    > Why doesn't the compiler give error for the absence of copy ctor
    > similar to error for the absence of default ctor.


    Because the generation of default ctor and copy ctor are independent of
    each other. If you declare any ctor (even a copy ctor) the compiler wont
    generate a default ctor. If you declare a copy ctor the compiler wont
    generate a default copy ctor, nor will it generate a default ctor.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 15, 2007
    #3
    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:
    504
    velthuijsen
    Apr 16, 2004
  2. giles

    Default Copy Ctor

    giles, Apr 12, 2005, in forum: C++
    Replies:
    4
    Views:
    335
    Ron Natalie
    Apr 13, 2005
  3. NVH
    Replies:
    8
    Views:
    473
    mlimber
    Jul 6, 2006
  4. Grizlyk
    Replies:
    8
    Views:
    455
    Grizlyk
    Nov 29, 2006
  5. puzzlecracker
    Replies:
    8
    Views:
    410
    James Kanze
    Apr 15, 2008
Loading...

Share This Page