|
-
March 19th, 2009, 09:47 AM
#1
CArray copy constructor
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?
-
March 19th, 2009, 09:57 AM
#2
Re: CArray copy constructor
 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
-
March 19th, 2009, 10:02 AM
#3
Re: CArray copy constructor
 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;
}
Victor Nijegorodov
-
March 19th, 2009, 10:04 AM
#4
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?
-
March 19th, 2009, 10:10 AM
#5
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;
}
};
Victor Nijegorodov
-
March 19th, 2009, 10:15 AM
#6
Re: CArray copy constructor
Hmm, maybe a vector is a good idea.
-
March 19th, 2009, 10:17 AM
#7
Re: CArray copy constructor
 Originally Posted by richiebabes
Hmm, maybe a vector is a good idea.
Perhaps...
Victor Nijegorodov
-
March 19th, 2009, 10:40 AM
#8
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].
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|