CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2004
    Posts
    474

    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?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CArray copy constructor

    Quote Originally Posted by richiebabes View Post
    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

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: CArray copy constructor

    Quote Originally Posted by richiebabes View Post
    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

  4. #4
    Join Date
    May 2004
    Posts
    474

    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?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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

  6. #6
    Join Date
    May 2004
    Posts
    474

    Re: CArray copy constructor

    Hmm, maybe a vector is a good idea.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: CArray copy constructor

    Quote Originally Posted by richiebabes View Post
    Hmm, maybe a vector is a good idea.
    Perhaps...
    Victor Nijegorodov

  8. #8
    Join Date
    May 2002
    Posts
    1,435

    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
  •  





Click Here to Expand Forum to Full Width

Featured