在union中可以使用类类型吗?

Julienjut 2001-12-04 08:49:15
如果可以为什么对象的方法不能调用?
...全文
174 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Julienjut 2001-12-12
  • 打赏
  • 举报
回复
谢谢do_do
给分了!
Julienjut 2001-12-10
  • 打赏
  • 举报
回复
回: do_do(do_do) 
哪 里去了?
Julienjut 2001-12-08
  • 打赏
  • 举报
回复
回: do_do(do_do) 
谢谢,我要的是思路,不要太多!
julienjut@163.com
Last_Dodo 2001-12-07
  • 打赏
  • 举报
回复
因工作须要,我做过一个类似的。我不能把它给你因为有违公司规定。
送个e-mail给我(david_f_deng@hotmail.com), 我给你送个间单的例子。
Julienjut 2001-12-06
  • 打赏
  • 举报
回复
谢谢,我已经找到了你的答案。
不过,有没有什么方法解决我如下的问题:
我需要在两台或更多个主机之间传递对象数据,这些对象有很多种,但每次只传递其中一个。当然数据打包时,有个包头包含了对象的基本信息。

1. It could save and waste space. If a union object is reused to hold different members, it could save space. Otherwise it most likely wastes space because it can only hold one value at a time. If the value is the biggest element, it does not save or waster space. If the value is not the biggest one, it actually waste space.
3. No matter what value it hold, they start at same address. That is the key advantage of union.
2. The reason of not allowing classes with contructor/destructor is to avoid memory corruption and leak. When an instance is being created, if members have constructors, they have to be called. Calling one constructor after another (from difference class) will lead to memory corruption. For example:
class A
{
public:
A() { _ip = new int; _i = 10; }
~A() { delete _ip; }
private:
int *_ip, _i;
};

class B
{
public:
B() { _i = 10; _ip = new int; }
~B() { delete _ip; }
private:
int _i, *_ip;
};

If you have a union with members of A and B, no matter which constructor is called first, the second constructor will corrupt the memory. By the same token, union members with destructor will cause memory problem too.

Julienjut 2001-12-06
  • 打赏
  • 举报
回复
回: do_do(do_do) 
请问你不是用的windows中文系统吗?否则怎么会不能用中文!
谢谢你的指点,不过好象都不太适用,原因如下:
这些类都不是我写的,而且一个类继承了多次,如果用查代码的方法来找出所有的成员变量,任务真的是很坚巨。如果现在只有类的指针,有没有什么方法可以实现?
你 smart packet handler class 开发包或者源码吗?
Last_Dodo 2001-12-06
  • 打赏
  • 举报
回复
有很多方法(今天终于可以打中文了,就是不熟,很慢):
1。用inserter把对象放到stream里去,和extractor从stream里重建对象.
2。写一函数传人缓冲或返回缓冲。
3。smart packet handler class。写一类专门把某些对象转换成char *.
等。
用union好象不太合适。
举例:
class A
{
public:
...
int putIntoBuffer(char *buf, int capacity) const;
char *putIntoBuffer(int &length) const;
...
private:
int _i;
float _f;
...
};

int A::putIntoBuffer(char *buf, int capacity) const
{
if (capacity < (sizeof(int)+sizeof(float)) return 0;
sprintf(buf, "%d%f", _i, _f);

return 1;
}

char *A::putIntoBuffer(int &length) const
{
length = sizeof(int)+sizeof(float);
char *buf = new char[length];
if (!buf){
length = -1;
return NULL;
}

sprintf(buf, "%d%f", _i, _f);
return buf;
}

Last_Dodo 2001-12-05
  • 打赏
  • 举报
回复
Yes, you can. But only the classes without user defined constructor or destructor are allowed. I have explained why the restriction is needed here earier this year. If you need it, I will try to explain it again.
lj9852 2001-12-05
  • 打赏
  • 举报
回复

哦,看错了,不好意思:(
不过应该可以吧
lj9852 2001-12-05
  • 打赏
  • 举报
回复

可以,就是所谓的“组合”
ccemail 2001-12-05
  • 打赏
  • 举报
回复
可以吧。但有必要吗?
Julienjut 2001-12-05
  • 打赏
  • 举报
回复
没有人碰到过类似的问题吗?
Julienjut 2001-12-04
  • 打赏
  • 举报
回复
是贵客总得留下点什么吧!

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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