copy constructor, assignment operator in template

Discussion in 'C++' started by Martin Vorbrodt, Sep 9, 2005.

  1. In "C++ Templates, The Complete Guide" i read that template copy-con is
    never default copy constructor, and template assignment-op is never a copy
    assignment operator. Could someone please explain how I could
    declate/override the two.

    Thanx
     
    Martin Vorbrodt, Sep 9, 2005
    #1
    1. Advertising

  2. Martin Vorbrodt wrote:
    > In "C++ Templates, The Complete Guide" i read that template copy-con is
    > never default copy constructor, and template assignment-op is never a copy
    > assignment operator. Could someone please explain how I could
    > declate/override the two.


    I am not sure how much you did understand, so, the text is talking about
    this situation

    struct A {
    template<class T> A(T const&); // "copy"-constructor
    };

    This "copy" constructor will not be used if 'T' is 'A'. The compiler will
    still generate another copy constructor,

    A(A const&);

    according to its usual rules of generating one, and will use it when copy
    construction is performed from another 'A' object.

    Same with the assignment op.

    To make sure you _do_ have control over the copy construction process from
    another 'A' object, define both the template and the non-template c-tors:

    struct A {
    template<class T> A(T const&);
    A(A const&);
    };

    and do your "true" copy-construction in the latter and "pseudo" copy
    construction in the former. Remember that there is no way to invoke one
    c-tor from another, so if you need to do some common processing, pull it
    out into a separate function.

    V
     
    Victor Bazarov, Sep 9, 2005
    #2
    1. Advertising

  3. what about this:

    template <typename T>
    class C {
    public:
    C(const T& t);
    C(const C& c); // DO I STILL NEED THIS IF T = C ?
    };



    "Victor Bazarov" <> wrote in message
    news:ShhUe.33871$01.us.to.verio.net...
    > Martin Vorbrodt wrote:
    > > In "C++ Templates, The Complete Guide" i read that template copy-con is
    > > never default copy constructor, and template assignment-op is never a

    copy
    > > assignment operator. Could someone please explain how I could
    > > declate/override the two.

    >
    > I am not sure how much you did understand, so, the text is talking about
    > this situation
    >
    > struct A {
    > template<class T> A(T const&); // "copy"-constructor
    > };
    >
    > This "copy" constructor will not be used if 'T' is 'A'. The compiler will
    > still generate another copy constructor,
    >
    > A(A const&);
    >
    > according to its usual rules of generating one, and will use it when copy
    > construction is performed from another 'A' object.
    >
    > Same with the assignment op.
    >
    > To make sure you _do_ have control over the copy construction process from
    > another 'A' object, define both the template and the non-template c-tors:
    >
    > struct A {
    > template<class T> A(T const&);
    > A(A const&);
    > };
    >
    > and do your "true" copy-construction in the latter and "pseudo" copy
    > construction in the former. Remember that there is no way to invoke one
    > c-tor from another, so if you need to do some common processing, pull it
    > out into a separate function.
    >
    > V
     
    Martin Vorbrodt, Sep 9, 2005
    #3
  4. Martin Vorbrodt wrote:
    > what about this:
    >
    > template <typename T>
    > class C {
    > public:
    > C(const T& t);
    > C(const C& c); // DO I STILL NEED THIS IF T = C ?


    Huh? T is C?

    > };
    >


    (a) Don't top-post.

    (b) How can you have a template whose argument is itself?

    > "Victor Bazarov" <> wrote
    > [...]


    V
     
    Victor Bazarov, Sep 9, 2005
    #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. Replies:
    12
    Views:
    2,236
    Howard
    Jun 16, 2005
  2. Replies:
    10
    Views:
    565
    Heinz Ozwirk
    Jul 5, 2005
  3. Ajay
    Replies:
    4
    Views:
    947
    Markus Becker
    May 12, 2006
  4. Henrik Goldman
    Replies:
    2
    Views:
    442
    Andrey Tarasevich
    Dec 26, 2006
  5. cinsk
    Replies:
    35
    Views:
    2,615
    James Kanze
    Oct 11, 2010
Loading...

Share This Page