Hi,
I understand that there must be a copy constructor if I
want to use an array for my class to become a collection.
This is the definition of my class:
class CEntity
{
public:
int num_inPort;
int num_outPort;
int entityCount;
CArray<CString, CString> inPortNameArray;
CEntity();
CEntity(const CEntity& initE);
virtual ~CEntity();
};
My copy constructor for my CEntity class is as follows:
CEntity::CEntity(const CEntity& initE)
{
num_inPort = initE.num_inPort;
num_outPort = initE.num_outPort;
entityCount = initE.entityCount;
int i=0;
int n=0;
while(i<inPortNameArray.GetSize())
{
inPortNameArray.ElementAt(i)
=initE.inPortNameArray.ElementAt(n);
i++;
n++;
}
}
However this produces an error error C2662: 'ElementAt' :
cannot convert 'this' pointer from 'const class
CArray<class CString,class CString>' to 'class
CArray<class CStr
ing,class CString> &'.
Can anyone enlgihten me on this problem? and what are the
solutions?
-----Original Message-----
Hi Ellie,
For example:
void CEntity::AddPortName(const CString strPortName){
inPortNameArray.Add(strPortName);
}
I don't know what you are trying to do with this class,
but the goal shouldbe to shift as much internal logic as possible into the
class itself. Thismethod might look trivial but it means you can change
the internal structureof CEntity without having to change code throughout the
rest of yourprogram.
Cheers
Doug Forster
"ellie" <an*******@discussions.microsoft.com> wrote in
messagenews:0b****************************@phx.gbl... What do you mean by a small accessor? Could you give an
example with reference to my instance?
>-----Original Message-----
>That will work, but is really bad style. You should
write a small accessor >method to add the value so you do not create
dependencies on the internal >structure of CEntity scattered all over your program.
inPortNameArray should >be private to enforce this. In fact I would never make
any member variables >public, though some may disagree with that.
>
>Cheers
>
>Doug Forster
>
>"MdAZ" <an*******@discussions.microsoft.com> wrote in
message >news:889CE7E2-59BB-4916-837E-
A1**********@microsoft.com... >> Try:
>>
>> R1.inPortNameArray.Add("V1");
>> R1.inPortNameArray.Add("V2");
>
>
>.
>
.