copy constructor, assignment operator in template

  • Thread starter Martin Vorbrodt
  • Start date
M

Martin Vorbrodt

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
 
V

Victor Bazarov

Martin said:
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
 
M

Martin Vorbrodt

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 ?
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top