L
lhr_cool_guy
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;
}
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;
}