CStringArray Copy Construtor

http://microsoft.ease.lsoft.com/scripts/wa-msn.exe?A2=ind9808a&L=mfc&T=0&O=D&P=2823

Date:         Mon, 3 Aug 1998 17:55:24 -0700
Reply-To:     MFC -- Microsoft Foundation Classes <[log in to unmask]>
Sender:       MFC -- Microsoft Foundation Classes <[log in to unmask]>
From:         [log in to unmask]
Subject:      Re: *CPtrArray & CStringArray()


Moderator's Direct Message/Digest
William Dempsey    [log in to unmask]

Mesg: 5B1CC0CE
From: Charan Kumar <[log in to unmask]>
Subj: RE: *CPtrArray & CStringArray()
Date: Mon, 3 Aug 1998 01:27:50 -0400

>Can any one tell me is there any direct method to assign an existing
>CStringArray or CPtrArray to another variabe of the respective types. I get
>the error message" no copy constructor available".

You can use Copy function which is applicable to CPtrArray & CStringArray.
Try the following sample program.

    CStringArray objArr1, objArr2;

    objArr1.Add("Charan");
    objArr1.Add("Kiran");

    objArr2.Copy (objArr1);
    AfxMessageBox (objArr2[1]);

===== Digest Separator =====

Mesg: 783C2455
From: Robert Niemann <[log in to unmask]>
Subj: Re: *CPtrArray & CStringArray()
Date: Mon, 03 Aug 1998 08:32:45 +0200

The problem is that the compiler does not know how to copy the classes.
Normanny if you run in this pronblem with your own code you just supply a copy
constructor (that is, a constructor that takes a reference to its own class:
CMyClass( CMyClass& rOther ) ). This constructor does what is needed to be
done for initializing. The problem with the collection classes is that they do
memory management. To pass them by value the memory has to be allocated and
the elements have to be copied. This could be expensive so the copy
constructors are not implemented by Microsoft. There are two resolutions for
your problem:

If you only need to _LOOK_UP_ of your array just declare:

int SetSata( CStringArray& raColNames, CPtrArray& raColValues );

REMEMBER that you will get references! If you modify your collection
(add, insert, delete elements) these modifications will be visible to
the outside world.

If you really need to _COPY_ your collection just derive your own class form
the collections and simply add a copy constructor:

class CMyStringArray : public CStringArray
{
public:
    DECLARE_SERIAL( CMyStringArray )
    CMyStringArray();
    CMyStringArray( CStringArray& rOther );
    ~CMyStringArray()
};

Remember that the process of copying may fail (low memory conditions). You
shold consider the possibility of throwing exceptions or to have a (BOOL)
method to verify the success of the copy process.

Whenever possible you should use references.

By the way: How did you manage to call your function? There would be the same
error messages!

===== Digest Separator =====

Mesg: 30615918
From: GOPISETTY, ACHYUTHA <[log in to unmask]>
Subj: RE: *CPtrArray & CStringArray()
Date: Mon, 3 Aug 1998 10:58:53 -0400

Use GetData(const CStringArray &aColNames, const CPtrArray &aColValues)
instead. Moderator, please avoid basic C++ questions in this advanced mailing
list.

原文地址:https://www.cnblogs.com/cy163/p/332378.html