Re: CArray copy constructor
Quote:
Originally Posted by
richiebabes
I have a struct:
Code:
struct shape
{
CString sName;
CArray <CPoint, CPoint> ptPoints;
int nSides;
};
If I try and do something like myshape->ptPoints = ptPointsList; I get an error. I have the feeling I have to write a copy constructor but haven't written one for donkey's years. Can anyone please help?
You could save yourself a lot of headaches and just use std::vector<CPoint>, which is already copyable.
Regards,
Paul McKenzie
Re: CArray copy constructor
Quote:
Originally Posted by
richiebabes
IIf I try and do something like myshape->ptPoints = ptPointsList; I get an error. I have the feeling I have to write a copy constructor...
For this assignment you need an operator = () method.
Derive a new class (say, CMyArray) from CArray <CPoint, CPoint> and then add operator =(). Something like:
Code:
class CMyArray : public CArray <CPoint, CPoint>
{
.....
CMyArray& operator =(const CMyArray& other);
}
....
CMyArray& CMyArray::operator =(const CMyArray& other)
{
RemoveAll();
Append(other);
return *this;
}
Re: CArray copy constructor
Yes I know, but I want to remind myself of how to write a copy constructor in this instance.
Anyone else can help?
Re: CArray copy constructor
In what instance?
If you meant copy ctor in your struct shape then it is easy:
Code:
struct shape
{
CString sName;
CMyArray ptPoints;
int nSides;
shape(const shape& other)
{
sName = other->sName;
ptPoints = other->ptPoints;
nSides = other->nSides;
}
};
Re: CArray copy constructor
Hmm, maybe a vector is a good idea.
Re: CArray copy constructor
Quote:
Originally Posted by
richiebabes
Hmm, maybe a vector is a good idea.
Perhaps... :rolleyes:
Re: CArray copy constructor
You can't write a copy constructor for a pre-existing MFC class that's already been compiled and linked into a dll. In other words, you cannot add a copy constructor to CArray. I have no idea why Microsoft left this class incomplete.
The best idea is to use a vector (as already suggested), but if you must use CArray then you will have to manually copy it by (1) setting size of destination to size of source, (2) loop through the elements and assign source[i] to destination[i].