473,811 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy Constructor with Template Class

Hello,

I am rather lost about how to properly setup a copy constructors and
operator= with my template class. My class if as follows:

template<class Tclass Image
{
private:
IplImage* imgp;

public:
Image(IplImage* img = NULL) { imgp = img; }
~Image()
{
if(imgp != 0)
cvReleaseImage( &imgp);
imgp = NULL;
}

IplImage* Ptr() { return imgp; }

void operator=(IplIm age* img)
{
imgp = img;
}

// copy construction
Image(const T& rhs);

T& operator=(const T& rhs);
};
template<class T>
Image<T>::Image (const T& rhs)
{
if(rhs.imgp != NULL && rhs.m_bReleaseM emory)
cvReleaseImage( &rhs.imgp);

imgp = cvCreateImage(c vGetSize(rhs.Pt r()), rhs.Ptr().depth ,
rhs.Ptr().nChan nels);
cvCopyImage(rhs .Ptr(), imgp);
}

template<class T>
T& Image<T>::opera tor=(const T& rhs)
{
if ( &rhs == this )
return *this;

if(imgp != NULL &&m_bReleaseMem ory)
cvReleaseImage( &imgp);

imgp = cvCreateImage(c vGetSize(rhs.Pt r()), rhs.Ptr().depth ,
rhs.Ptr().nChan nels);
cvCopyImage(rhs .Ptr(), imgp);
}

I am confused about two points. First, although I can compile the
above code, I can not set a break point in either the
Image<T>::Image (const T& rhs) or T& Image<T>::opera tor=(const T& rhs)
functions. My compiler (VS 8.0) indicates the break points will never
be hit. Secondly, is I am uncertain if they is sufficient to solve the
following problem:

vector< Image<unsigned char v;

void main()
{
void Foo();
v.at(0).size; // error: memory has already been freed!
}

void Foo()
{
Image<unsigned charimage;
IplImage iplImage = new IplImage();
image = iplImage;

v.push_back(ima ge);
}

Obviously this will not work since I am allocating "Image<unsi gned
charimage" on the stack. It will thus free its memory when the
function ends. I believe that properly overloading the copy
constructor and operator= will solve this problem, but am not sure how
to do this.

Thanks you for any and all help.

Cheers!

Sep 29 '07 #1
0 1462

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7178
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): <code:templates.h> #include <string> #include <iostream>
2
1754
by: Indrawati Yahya | last post by:
Hi I am having trouble in defining a templated class' copy constructor. Here is the simplest code that represent my problem: template<typename T1> class Foo { public: Foo(T1 bar = T1()): _bar(bar), _cached(false) {}
3
1929
by: Matt Bitten | last post by:
Hi, all. I have the same old problem about templates and copy constructors. I know this has been addressed hundreds of times, but despite perusing many old postings, and The Standard as well, I'm still feeling confused. Suppose I have a class template: template <typename T> class Foo {
4
3383
by: Matthias Spiller | last post by:
Hi, following problem. I'm writing an Image class and want to use templates: .... template<class TType, int TLayers> class Image { public: Image();
3
10282
by: Martin Vorbrodt | last post by:
In "C++ Templates, The Complete Guide" i read that template copy-con is never default copy constructor, and template assignment-op is never a copy assignment operator. Could someone please explain how I could declate/override the two. Thanx
13
2926
by: MurphyII | last post by:
Just a little sample : class A { public: A( ) { } template<typename T> A( const typename T& a) {
11
3439
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
7
1731
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum, short Trank, bool Tcont> TopTen<Tnum,Trank,Tcont>::TopTen( const TopTen<Tnum,Trank,Tcont& tten );
2
1707
by: dolphin | last post by:
Hi All Today , I read the source code of auto_ptr. I have a question about the copy constructor of auto_ptr. template<class _Ty> class auto_ptr { .................. } there is a copy constructor below
9
2906
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no Foo() is included, i believe. };
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10392
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...
0
10136
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
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
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
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.