copy constructor

Discussion in 'C++' started by Rahul, Nov 25, 2007.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    It is well known that the input parameter which is passed to the
    copy constructor is passed as reference and not as as object. Because
    passing an object is as good as making another copy which in itself
    needs a copy constructor.

    However i was wondering why can't the existing object be passed as
    a pointer instead of a reference to the copy constructor which creates
    a new object.

    Thanks in advance!!!
    Rahul, Nov 25, 2007
    #1
    1. Advertising

  2. Rahul

    Kai-Uwe Bux Guest

    Rahul wrote:

    > Hi Everyone,
    >
    > It is well known that the input parameter which is passed to the
    > copy constructor is passed as reference and not as as object. Because
    > passing an object is as good as making another copy which in itself
    > needs a copy constructor.
    >
    > However i was wondering why can't the existing object be passed as
    > a pointer instead of a reference to the copy constructor which creates
    > a new object.


    a) You _can_ define a constructor

    T ( T* ptr )
    : member_1 ( ptr->member_1 )
    , ...
    {}

    Although it will buy you nothing but trouble since you invite all the perils
    of undefined behavior upon you, should you pass a null pointer.


    b) Such a constructor is _not_ a copy constructor. The standard defines copy
    constructors as those that conform to certain signatures. Also, the
    standard says that at certain points copy constructors will be used, say,
    to create temporaries. That the constructor in (a) is not a copy
    constructor has therefore the consequence that it will not be considered
    for such purposes.


    That the copy-constructor is invoked automatically in certain contexts and
    can be optimized away in others creates a restriction on its semantics: it
    really better just construct a copy of the object. Thus, one problem with
    making (a) a valid signature for copy-constructors is that it renders it
    impossible to use a constructor like (a) for other purposes. E.g. a
    single-linked list might have a private constructor

    List ( List* tail_ptr = 0, ValueType const & value = ValueType() )


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 25, 2007
    #2
    1. Advertising

  3. Rahul:

    > It is well known that the input parameter which is passed to the
    > copy constructor is passed as reference and not as as object. Because
    > passing an object is as good as making another copy which in itself
    > needs a copy constructor.



    The terminology you're looking for is "pass by value" Vs "pass by
    reference".

    By value: void Func(MyClass obj); /* New object created within the
    function */

    By reference: void Func(MyClass &obj); /* Same external object */

    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Nov 25, 2007
    #3
  4. Rahul

    Rahul Guest

    On Nov 25, 10:34 am, Kai-Uwe Bux <> wrote:
    > Rahul wrote:
    > > Hi Everyone,

    >
    > > It is well known that the input parameter which is passed to the
    > > copy constructor is passed as reference and not as as object. Because
    > > passing an object is as good as making another copy which in itself
    > > needs a copy constructor.

    >
    > > However i was wondering why can't the existing object be passed as
    > > a pointer instead of a reference to the copy constructor which creates
    > > a new object.

    >
    > a) You _can_ define a constructor
    >
    > T ( T* ptr )
    > : member_1 ( ptr->member_1 )
    > , ...
    > {}
    >
    > Although it will buy you nothing but trouble since you invite all the perils
    > of undefined behavior upon you, should you pass a null pointer.
    >
    > b) Such a constructor is _not_ a copy constructor. The standard defines copy
    > constructors as those that conform to certain signatures. Also, the
    > standard says that at certain points copy constructors will be used, say,
    > to create temporaries. That the constructor in (a) is not a copy
    > constructor has therefore the consequence that it will not be considered
    > for such purposes.
    >


    Yes, it wouldn't be called as that wouldn't be a copy
    constructor as per current c++ implementation and standards. Howevre,
    my question was at the design level in c++ as to why reference was
    decided to pass to the copy constructor and why wasn't a pointer
    considered?

    > That the copy-constructor is invoked automatically in certain contexts and
    > can be optimized away in others creates a restriction on its semantics: it
    > really better just construct a copy of the object. Thus, one problem with
    > making (a) a valid signature for copy-constructors is that it renders it
    > impossible to use a constructor like (a) for other purposes. E.g. a
    > single-linked list might have a private constructor
    >
    > List ( List* tail_ptr = 0, ValueType const & value = ValueType() )
    >
    > Best
    >
    > Kai-Uwe Bux
    Rahul, Nov 26, 2007
    #4
  5. Rahul

    Kai-Uwe Bux Guest

    Rahul wrote:

    > On Nov 25, 10:34 am, Kai-Uwe Bux <> wrote:
    >> Rahul wrote:
    >> > Hi Everyone,

    >>
    >> > It is well known that the input parameter which is passed to the
    >> > copy constructor is passed as reference and not as as object. Because
    >> > passing an object is as good as making another copy which in itself
    >> > needs a copy constructor.

    >>
    >> > However i was wondering why can't the existing object be passed as
    >> > a pointer instead of a reference to the copy constructor which creates
    >> > a new object.

    >>
    >> a) You _can_ define a constructor
    >>
    >> T ( T* ptr )
    >> : member_1 ( ptr->member_1 )
    >> , ...
    >> {}
    >>
    >> Although it will buy you nothing but trouble since you invite all the
    >> perils of undefined behavior upon you, should you pass a null pointer.
    >>
    >> b) Such a constructor is _not_ a copy constructor. The standard defines
    >> copy constructors as those that conform to certain signatures. Also, the
    >> standard says that at certain points copy constructors will be used, say,
    >> to create temporaries. That the constructor in (a) is not a copy
    >> constructor has therefore the consequence that it will not be considered
    >> for such purposes.
    >>

    >
    > Yes, it wouldn't be called as that wouldn't be a copy
    > constructor as per current c++ implementation and standards. Howevre,
    > my question was at the design level in c++ as to why reference was
    > decided to pass to the copy constructor and why wasn't a pointer
    > considered?


    Please read the remainder of my previous post. It addresses exactly that
    question.

    >> That the copy-constructor is invoked automatically in certain contexts
    >> and can be optimized away in others creates a restriction on its
    >> semantics: it really better just construct a copy of the object. Thus,
    >> one problem with making (a) a valid signature for copy-constructors is
    >> that it renders it impossible to use a constructor like (a) for other
    >> purposes. E.g. a single-linked list might have a private constructor
    >>
    >> List ( List* tail_ptr = 0, ValueType const & value = ValueType() )
    >>


    [snip]


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 26, 2007
    #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. VisionSet
    Replies:
    8
    Views:
    4,878
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    457
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    560
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,204
  5. cinsk
    Replies:
    35
    Views:
    2,565
    James Kanze
    Oct 11, 2010
Loading...

Share This Page