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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值