D
Donovan Parks
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 T> class 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=(IplImage* 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_bReleaseMemory)
cvReleaseImage(&rhs.imgp);
imgp = cvCreateImage(cvGetSize(rhs.Ptr()), rhs.Ptr().depth ,
rhs.Ptr().nChannels);
cvCopyImage(rhs.Ptr(), imgp);
}
template<class T>
T& Image<T>:perator=(const T& rhs)
{
if ( &rhs == this )
return *this;
if(imgp != NULL &&m_bReleaseMemory)
cvReleaseImage(&imgp);
imgp = cvCreateImage(cvGetSize(rhs.Ptr()), rhs.Ptr().depth ,
rhs.Ptr().nChannels);
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>:perator=(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 char> image;
IplImage iplImage = new IplImage();
image = iplImage;
v.push_back(image);
}
Obviously this will not work since I am allocating "Image<unsigned
char> image" 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!
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 T> class 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=(IplImage* 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_bReleaseMemory)
cvReleaseImage(&rhs.imgp);
imgp = cvCreateImage(cvGetSize(rhs.Ptr()), rhs.Ptr().depth ,
rhs.Ptr().nChannels);
cvCopyImage(rhs.Ptr(), imgp);
}
template<class T>
T& Image<T>:perator=(const T& rhs)
{
if ( &rhs == this )
return *this;
if(imgp != NULL &&m_bReleaseMemory)
cvReleaseImage(&imgp);
imgp = cvCreateImage(cvGetSize(rhs.Ptr()), rhs.Ptr().depth ,
rhs.Ptr().nChannels);
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>:perator=(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 char> image;
IplImage iplImage = new IplImage();
image = iplImage;
v.push_back(image);
}
Obviously this will not work since I am allocating "Image<unsigned
char> image" 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!