473,804 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

templatized copy constructor not being deduced

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
3 1518
lh**********@ya hoo.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
On 26 Dec 2005 07:04:15 -0800, lh**********@ya hoo.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
Zara <yo****@terra.e s> writes:
On 26 Dec 2005 07:04:15 -0800, lh**********@ya hoo.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2980
by: hall | last post by:
I have a problem with my design of a templatized class. I'm trying to figure out how to load and save the data inside it, but can't. My class looks like this ------------------------------------ template <class tType> class Foo { private: tType data;
42
5816
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
15
21206
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including pointers) and for objects types call their default constructor. Any others points i should know?
1
1470
by: aurgathor | last post by:
Howdy, I got the templatized class Matrix working in the way it's written in the faq , and it works fine as: Matrix<sqr_T> display(80,25); However, I'd like to have this variable in the private section of another class, and I'd like to instantiate in the class's constructor, with the option of having a size determined at run-time.
5
7368
by: mrstephengross | last post by:
Ok, I've got code that looks something like this: ================================================== template<typename T1, typename T2> class Base { public: explicit Base(const T1 & t1) { /* ... */ } };
23
3871
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
1
1868
by: tavianator | last post by:
If I had a class with a templatized constructor, like this: class foo { public: template<typename T> foo(); };
3
3291
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do this non-intrusively -- I.e., without, for example, typedef'ing the types in every compilation unit? Background: Our code base has assert macros that need to reboot the system after notifying components via a single templatized Component...
4
1759
by: Zachary Turner | last post by:
Is it possible to declare a class with a templatized constructor? I was sure it was, but GCC is giving me errors when I try to do this. Most likely user error. Some example code is: class MessageHeader { private: int m_count; public:
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.