类中的数组 [英] Arrays within classes

查看:98
本文介绍了类中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将元素添加到

另一个类中定义的数组?


例如,


class CEntity

{

public:

int num_inPort;

int num_outPort;

int entityCount;

char * inPortName [100];

char * outPortName [100];

CArray< CString,CString&> ; inPortNameArray;

}


CEntity R1;

R1.num_inPort = 1;

R1。 num_outPort = 1;

R1.entityCount = 1;

//如何在这里添加数组信息?如果让我们说

数组中的元素是v1和v2。

How do you add elements to an array defined within
another class?

For instance,

class CEntity
{
public:
int num_inPort;
int num_outPort;
int entityCount;
char* inPortName[100];
char* outPortName[100];
CArray<CString, CString&> inPortNameArray;
}

CEntity R1;
R1.num_inPort = 1;
R1.num_outPort = 1;
R1.entityCount = 1;
//how to add the array information here? if let''s say
elements in the array are "v1" and "v2".

推荐答案

尝试:


R1.inPortNameArray.Add(" V1" ;);

R1.inPortNameArray.Add(" V2");
Try:

R1.inPortNameArray.Add("V1");
R1.inPortNameArray.Add("V2");


这会有效,但风格真的很糟糕。你应该编写一个小的访问器

方法来添加这个值,这样你就不会在整个程序中分散的CEntity的内部结构上创建依赖关系。 inPortNameArray应该是私有的,以强制执行此操作。事实上,我永远不会公开任何会员变量

,尽管有些人可能不同意。


干杯


Doug Forster


" MdAZ" <一个******* @ discussions.microsoft.com>在留言中写道

news:88 ********************************** @ microsof t.com ...
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:88**********************************@microsof t.com...
尝试:

R1.inPortNameArray.Add(" V1");
R1.inPortNameArray.Add(" V2" );
Try:

R1.inPortNameArray.Add("V1");
R1.inPortNameArray.Add("V2");



小型存取器是什么意思?你可以参考我的实例给出一个

的例子吗?

What do you mean by a small accessor? Could you give an
example with reference to my instance?

-----原始消息-----
那会奏效,但风格真的很糟糕。你应该
编写一个小的accessor方法来添加值,这样你就不会在整个程序中散布的CEntity的内部结构上创建
依赖项。
inPortNameArray应该是私有的,以强制执行此操作。事实上,我永远不会让
任何成员变量公共,但有些人可能不同意。

干杯

道格福斯特

" MdAZ" <一个******* @ discussions.microsoft.com>写在
messagenews:889CE7E2-59BB-4916-837E -
-----Original Message-----
That will work, but is really bad style. You should write a small accessormethod to add the value so you do not create dependencies on the internalstructure of CEntity scattered all over your program. inPortNameArray shouldbe private to enforce this. In fact I would never make any member variablespublic, though some may disagree with that.

Cheers

Doug Forster

"MdAZ" <an*******@discussions.microsoft.com> wrote in messagenews:889CE7E2-59BB-4916-837E-


A1 * *********@microsoft.com ...

尝试:

R1.inPortNameArray。添加(V1);
R1.inPortNameArray.Add(" V2");
Try:

R1.inPortNameArray.Add("V1");
R1.inPortNameArray.Add("V2");




.



这篇关于类中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆