Sign in to follow this  

Copy Constructor and Templates

This topic is 3331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I found the following on Wikipedia about the C++ copy constructor : "Contrary to expectations, a template copy constructor is not an explicit copy constructor." (Article link) Is this true? I created the following test program in Visual Studio 2005, and it works fine.
template <typename T, int num_items>
class cTest
{
	public :

		const int	m_num_items ;
		T *		m_items ;

		cTest () : m_num_items (num_items)
		{
			m_items = new T [num_items] ;
		}

		~cTest ()
		{
			delete [] m_items ;
		}

		cTest (const cTest &source) : m_num_items (source.m_num_items)
		{
			m_items = new T [num_items] ;
			for (int k = 0 ; k < m_num_items ; k++)
				m_items [k] = source.m_items [k] ;
		}
} ;



int main (int argc, char *argv [])
{
	cTest<int, 4> t1 ;
	cTest<int, 4> t2 (t1) ;

	return 0 ;
}






Share this post


Link to post
Share on other sites
What you have is a non-template copy constructor of a template class. What the article is referring to is template copy constructors. Ex:

template <typename U>
cTest(const U & something) {
// stuff
}

Share this post


Link to post
Share on other sites
Yes, sorry, that is something different. Is that why the following does not compile?


template <typename T, int num_items>
class cTest
{
public :

const int m_num_items ;
T * m_items ;

cTest () : m_num_items (num_items)
{
m_items = new T [num_items] ;
}

~cTest ()
{
delete [] m_items ;
}

cTest (const T &item) : m_num_items (1)
{
m_items = new T [1] ;
m_items [0] = T ; // error C2275: 'T' : illegal use of this type as an expression
}
} ;




int main (int argc, char *argv [])
{
cTest<int, 1> t1 ;
cTest<int, 1> t2 (12) ;

return 0 ;
}

Share this post


Link to post
Share on other sites
Yes, I must be sleeping. I meant the following, which works:


template <typename T, int num_items>
class cTest
{
public :

const int m_num_items ;
T * m_items ;

cTest () : m_num_items (num_items)
{
m_items = new T [num_items] ;
}

~cTest ()
{
delete [] m_items ;
}

cTest (const T &item) : m_num_items (1)
{
m_items = new T [1] ;
m_items [0] = item ;
}
} ;


int main (int argc, char *argv [])
{
cTest<int, 1> t1 ;
cTest<int, 1> t2 (12) ;

return 0 ;
}

Share this post


Link to post
Share on other sites
I get it now. The thing is that when you have

template <typename T> cTest::cTest <const T &source) { //... }

and you use cTest for T, you get

cTest::cTest <const cTest &source) { //... }

which looks a _lot_ like a copy constructor for cTest, but isn't.


Share this post


Link to post
Share on other sites

This topic is 3331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this