By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

templatized copy constructor not being deduced

P: n/a
The following code is compiling correctly on Visual Studio .NET 2003
but crashes. The problem is that the copy constructor is not being
called when it should be. Am I doing something wrong or is the compiler
at fault? I don't have any other compilers on which I could test this
:( If it IS a problem in the compiler, are there any workarounds?
template< class T >
class C
{
T * p;

public:

C();

template< class U >
C( const C< U > & ref );

~C();
};

template< class T >
C< T >::C< T >()
{
p = new double[3*4];
}

template< class T >
template< class U >
C< T >::C< T >( const C< U > & ref )
{
p = new double[3*4];
}

template< class T >
C< T >::~C< T >()
{
if ( p )
delete [] p;
}

template< class T >
C< T > inv( const C< T > & ref )
{
C< T > c;

return c;
}

int main()
{
C< double > c1, c2;

c2 = inv( c1 );

return 0;
}

Dec 26 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
lh**********@yahoo.com wrote:
template< class T >
C< T >::~C< T >()
{
if ( p )
The above test is superfluous, delete already does that.
delete [] p;
} int main()
{
C< double > c1, c2;
c2 = inv( c1 );
This invokes the assignment operator, not the copy constructor. c1
and c2 now hold the same value for p, so a crashing double-delete
occurs at the end of the block.
return 0;
}

Martin

--
Quidquid latine scriptum sit, altum viditur.
Dec 26 '05 #2

P: n/a
On 26 Dec 2005 07:04:15 -0800, lh**********@yahoo.com wrote:
The following code is compiling correctly on Visual Studio .NET 2003
but crashes. The problem is that the copy constructor is not being
called when it should be. Am I doing something wrong or is the compiler
at fault? I don't have any other compilers on which I could test this
:( If it IS a problem in the compiler, are there any workarounds?


Some notes:

- if you provide one of: virtual destructor, copy constructor, copy
assignment, you should usually provide the others.

- copy constructor and copy assigment will never be instanced from
templated comnstructors/ assignment. You should write them explicitly,
or the compiler will generate them for you. BTW, that *is* your
problem, and double delete the crashing symptom.

Best regards,

-- Zara
Dec 26 '05 #3

P: n/a
Zara <yo****@terra.es> writes:
On 26 Dec 2005 07:04:15 -0800, lh**********@yahoo.com wrote:
The following code is compiling correctly on Visual Studio .NET 2003
but crashes. The problem is that the copy constructor is not being
called when it should be. Am I doing something wrong or is the compiler
at fault? I don't have any other compilers on which I could test this
:( If it IS a problem in the compiler, are there any workarounds?

Some notes:

- if you provide one of: virtual destructor, copy constructor, copy
assignment, you should usually provide the others.


Almost.

If you provide one of: (non trivial) destructor, copy constructor, or
copy assignment you should usually provide the others.

It doesn't matter if destructor is virtual or not. But often you
provide an empty virtual destructor just to make it possible to delete
objects by pointers to base class, and then it is not necessary to
provide copy constructor and copy assignment operators.
- copy constructor and copy assigment will never be instanced from
templated comnstructors/ assignment. You should write them explicitly,
or the compiler will generate them for you. BTW, that *is* your
problem, and double delete the crashing symptom.


Agree.

/Niklas Norrthon
Dec 29 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.