如何动态对象属性生成?

baijigan 2007-01-13 02:19:23
如有个对象定义为
class CFun
{
int x;
};

这个对象只定义了属性x,但是如何能够在使用的时候可以动态建一个y出来

CFun* fun= new CFun;
fun->Dyna(); // 这里是否可以通过一个方法来动态建出y来?
int y= fun->y;
...全文
294 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
baijigan 2007-01-17
  • 打赏
  • 举报
回复
还有方案吗?
taodm 2007-01-16
  • 打赏
  • 举报
回复
楼主,你还是换Python吧。要记住,C++永远是C++。
DragonBill 2007-01-16
  • 打赏
  • 举报
回复
用动态生成属性干嘛??
baijigan 2007-01-16
  • 打赏
  • 举报
回复
我有n组数据,形式为key= value的形式, 打包在一个对象中,也就是如下:
class obj
{
key1= value1;
key2= value2;
.....
}

这些数据的key名字现在是不知道的(obj的名字也是不知道的),而且将来也不会直接写到程序代码中,而是通过
文件中读取到的.

现在没有数据先写处理程序, 所以希望有个形式能够预先有个对象obj
在处理程序中直接写成obj.key1形式来访问数据.

当然了,如果是写个方法Val()来实现成obj->val("key")的形式很容易的.
不想这样,还是希望obj.key的形式来直观表示.

尝试了模板\宏定义都没有实现.
sirguan 2007-01-15
  • 打赏
  • 举报
回复
为什么会有这种需求呢?
taodm 2007-01-15
  • 打赏
  • 举报
回复
说说你的原始需求。
baijigan 2007-01-15
  • 打赏
  • 举报
回复
各位还有什么方法吗?

我要的只是假象成“对象.变量”的形式,也就是可以 通过 对象.变量的形式来访问,
变量的名字是任意的,不可定的.
jixingzhong 2007-01-14
  • 打赏
  • 举报
回复
另外一种方法可能更合适,
就是使用 设计模式 之工厂模式。

根据实际的需要,
我们可以生成指定的类型,
当然这个就要求事先定义好可能的类型了 ······

这样会比较安全、方便,但是不易于扩展。

如果要考虑扩展问题(比如出现新的类型)
可以考虑使用 抽象工厂方法模式 ···
jixingzhong 2007-01-14
  • 打赏
  • 举报
回复
附: 灵活数组成员资料:

结构体变长的妙用——0个元素的数组
有时我们需要产生一个结构体,实现了一种可变长度的结构。如何来实现呢?
看这个结构体的定义:
typedef struct st_type
{
int nCnt;
int item[0];
}type_a;
(有些编译器会报错无法编译可以改成:)
typedef struct st_type
{
int nCnt;
int item[];
}type_a;
这样我们就可以定义一个可变长的结构,用sizeof(type_a)得到的只有4,就是sizeof(nCnt)=sizeof(int)那个0个元素的数组没有占用空间,而后我们可以进行变长操作了。
C语言版:
type_a *p = (type_a*)malloc(sizeof(type_a)+100*sizeof(int));
C++语言版:
type_a *p = (type_a*)new char[sizeof(type_a)+100*sizeof(int)];
这样我们就产生了一个长为100的type_a类型的东西用p->item[n]就能简单地访问可变长元素,原理十分简单,分配了比sizeof(type_a)多的内存后int item[];就有了其意义了,它指向的是int nCnt;后面的内容,是没有内存需要的,而在分配时多分配的内存就可以由其来操控,是个十分好用的技巧。
而释放同样简单:
C语言版:
free(p);
C++语言版:
delete []p;
其实这个叫灵活数组成员(fleible array member)C89不支持这种东西,C99把它作为一种特例加入了标准。但是,C99所支持的是incomplete type,而不是zero array,形同int item[0];这种形式是非法的,C99支持的形式是形同int item[];只不过有些编译器把int item[0];作为非标准扩展来支持,而且在C99发布之前已经有了这种非标准扩展了,C99发布之后,有些编译器把两者合而为一。
下面是C99中的相关内容:
6.7.2.1 Structure and union specifiers

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. With two exceptions, the flexible array member is ignored. First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106) Second, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.
例如在VC++6里使用两者之一都能通过编译并且完成操作,而会产生warning C4200: nonstandard extension used : zero-sized array in struct/union的警告消息。
而在DEVCPP里两者同样可以使用,并且不会有警告消息。
jixingzhong 2007-01-14
  • 打赏
  • 举报
回复
一种办法,
用 C99 定义的灵活数组成员,
然后根据需要分配一些空间,
并根据实际需要操作的类型对各个元素进行操作 ·····

这样的话,
1 是比较麻烦
2 是比较危险,需要对各种数据的内存布局有足够的了解,
否则极易出错
lonelyforest 2007-01-14
  • 打赏
  • 举报
回复
直接生成算了, 省得麻烦
kenneth_lueng 2007-01-14
  • 打赏
  • 举报
回复
怎么感觉是在拷贝构造呢?

femalelover 2007-01-14
  • 打赏
  • 举报
回复
当产生这种需要的时候, 可能就是使用继承的时候了.
v2002750 2007-01-14
  • 打赏
  • 举报
回复
听上去是奇怪的语言特性呢,C++有吗?
不过这种不确定性真的有必要吗?楼主不如先把你需要解决的问题说出来看看。
baijigan 2007-01-14
  • 打赏
  • 举报
回复
OOPhaisky(异化$渴望成功~~) 说的对!

事实是不可能动态生成成员变量,需要只是假象成“对象.变量”的形式
OOPhaisky 2007-01-14
  • 打赏
  • 举报
回复
如何能够在使用的时候可以动态建一个y出来
------------------------------------------------------------------------------------
无解。

PS:只能利用一些技巧,但是你并没有“动态添加数据成员”,仅仅是假象而已。
caocheng8230 2007-01-13
  • 打赏
  • 举报
回复
class CFun
{
int x;
public:
int*Dyna()
{
int*y=new int;
....
return y;
}
};

CFun* fun= new CFun;
int *y=fun->Dyna(); // 这里是否可以通过一个方法来动态建出y来?
这里的y就由Dyna()构建出来.
caocheng8230 2007-01-13
  • 打赏
  • 举报
回复
建议楼主可以参考一下Prototype的模式,多态的模式,就是楼主想要的设计方法.
vincentcsdn 2007-01-13
  • 打赏
  • 举报
回复
类的大小在编译期间就必须确定下来,不能在运行期间再去分配类的大小

可以这样
class CFun
{
int x;
int* y; //填一个指针下去,确定好类的大小,运行期间用 new 再给 y分配空间。要记得释放 y的空间哦
};

这样做其实对 4字节以下的数据类型是毫无意义的。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧