copy constructor template class

I am trying to use a copy constructor with a template class.

Here is the prototype:

Vector(const Vector<generic>& x);

Here is the implementation:

template <class generic>
Vector<generic>::Vector(const Vector<generic>&x)
{
*this = x;
}

= has been overloaded as follows

Vector<generic>& Vector<generic>::operator=(const Vector<generic> & x)
{
m_size = 0;
m_maxSize = 1;
for (unsigned int i = 0; i < x.size(); i++)
{
pushBack(x[i]);
}
return *this;
}

the overloaded = has been tested successfully in a unit test.

Here is the call to the copy constructor

v3(v1); //copy contents of v1 to v3


Here is the compiler error:

testVector.cpp: In member function ‘void TestVector::testPushBack()’:
testVector.cpp:50: error: no match for call to ‘(Vector<int>) (Vector<int>&)’


What is the problem?


So you're doing something like this :
1
2
Vector<int> v1;
Vector<int> v3(v1); 

?
If you do it like that, and this doesn't want to compile, then post more code.
Last edited on
The problem is that:

 
v3( v1 );


is not a call to the copy constructor; it is a call to operator() [the function call operator] with a
parameter of v1.

You need to do as R0mai typed.

BTW, as a matter of good practice, it is not a good idea to implement copy construction by
way of assignment; rather, for exception safety reasons, the reverse is usually done.
for exception safety reasons, the reverse is usually done
Would you mind elaborating?
Last edited on
Coincidentally, I am working with ATL right now. Examining code of CComVariant to make sure I was doing things right, I found out that at least one of the constructors make use of the overloaded operator=():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
	CComVariant(_In_ const CComBSTR& bstrSrc)
	{
		vt = VT_EMPTY;
		*this = bstrSrc;
	}
	CComVariant& operator=(_In_ const CComBSTR& bstrSrc)
	{
		Clear();
		vt = VT_BSTR;
		bstrVal = bstrSrc.Copy();
#pragma warning(push)
#pragma warning(disable:4068)
#pragma prefast(push)
#pragma prefast(disable:325, "We are checking allocation semantics here")
		if (bstrVal == NULL && bstrSrc.m_str != NULL)
		{
			vt = VT_ERROR;
			scode = E_OUTOFMEMORY;
#ifndef _ATL_NO_VARIANT_THROW
			AtlThrow(E_OUTOFMEMORY);
#endif
		}
#pragma prefast(pop)
#pragma warning(pop)
		return *this;
	}


Therefore, I am now interested as well to hear why the reverse is usually done. A quick search found nothing relevant, but again, I may be looking in the wrong places... er... keywords. :-P
OH!!!!! I finally get what a copy constructor does!

Thanks so much! Compiles fine.
A little late,

But I do not believe templated copy constructors are allowed, or atleast work for all situations,

I.e. if class "Generic" is an array of pointers, the copy would fail to produce the correct results as it would if "Generic" were an int. So for templated class, it may be best to have explicitly defined copy constructor for odd cases (i.e. non-predefined types).
The thing is, the copy constructor for T1<T2> will only take a T1<T2> as its parameter rather than T1<T3>. If copy constructors for template classes weren't allowed, the entire STL could not exist.
I think iharrold might have been referring to making the copy constructor itself a templated constructor.
Which is of course not allowed, and even nonsensical, since a copy constructor has a very specific
signature.

I will try to write an article about copy constructors and how to build exception safe assignment operators
(via copy-swap).
The implementations are illogical. You've written the assignment operator with the assumption that it is only used for copy construction which is wrong. It doesn't check for self assignment, doesn't clear the LHS container, and so forth. The implementation is conceptually wrong. As far as the copy/swap idiom goes that is another story. You could use that but don't have to. first you have to study the concept of the assignment operator so that you understand every circumstance for which it is needed. What you currently have is totally wrong for a variety of reasons.
An article on the copy/swap idiom would be an excellent addition to the articles database.
Topic archived. No new replies allowed.