hjack 发表于 2008-5-15 14:04

类型转换问题

class Base
{
public:
   Base():_x(111){}
private:
   int _x;
};

class Derive : public Base
{
public:
   Derive():_y(222){}
private:
   int _y;
};

int main()
{
   queue<Base> q;
   Derive d;
   q.push(d);
   
   ....
   
   Base b = q.front();//这样是可以的.
   
   return 0;
}

q.front();取出来的是Base类型的对象.
怎样才可以转为正确的类型?
比如这里,我要得到Derive的对象,并会访问该对象的_y成员.
除了dynamic_cast外还有其它方法吗?

iptton 发表于 2008-5-15 14:13

_x , _y 不都是 private的吗...

hjack 发表于 2008-5-15 14:21

这不是重点,可以设为public。
还有个问题是,queue<Base>定义的是一个存放Base对象的queue,我想这就是限制之所在,存入队列的对象已被缩小为Base了。也就是信息丢失。
有什么办法呢?

iptton 发表于 2008-5-15 14:22


#include <iostream>
#include <queue>
using namespace std;
class Base
{
public:
   Base():_x(111){}
   virtual int getValue(){return _x;}
private:
   int _x;
};

class Derive : public Base
{
public:
   Derive():_y(222){}
   virtual int getValue(){return _y;}
private:
   int _y;
};

int main()
{
   queue<Base*> q;
   Derive d;
   q.push(&d);
   

   
   Base *b = q.front();//这样是可以的.
   
   cout<<b->getValue()<<endl;
   
   return 0;
}

iptton 发表于 2008-5-15 14:23

我错了...

iptton 发表于 2008-5-15 14:25

记得看过 effective stl 里说,不要往容器里放对象,而是放指针...

iptton 发表于 2008-5-15 14:34

我觉得问题在这句:
Base b= q.front();

就算写成
Base b=d;
一样会被向下转换丢失信息

hjack 发表于 2008-5-15 14:39

Right.
But I don't want to use dynamic_cast....

hjack 发表于 2008-5-15 14:57

There are several threads to update the queue.
If I save the pointer in the queue, the real object the pointer points maybe a local data.
Could you please give some suggestion how to save the data the pointer points?

iptton 发表于 2008-5-15 16:19

又是跨进程操作?

hjack 发表于 2008-5-15 17:00

No, just inter-thread.

iptton 发表于 2008-5-15 17:40

There are several threads to update the queue.
If I save the pointer in the queue, the real object the pointer points maybe a local data.

用new 不行吗?
用了new后,存放在容器里MS要用到 shared_point 等 智能指针...

iptton 发表于 2008-5-15 17:42

准确点说,好像看到过些建议是放进容器里的指针都应该用智能指针..

hjack 发表于 2008-5-15 17:52

I am going to use new.
And provide a method to new an object to return its pointer.
User can use this pointer to enqueue.
When dequeue, delete operation will be done.
Avoid that there is unmatching new/delete in user's code.
Any good idea??
By the way, it seems that it cannot be an auto_ptr in a container.

iptton 发表于 2008-5-15 18:02

hjack 现在在公司说英语?

用auto_point会出问题..看过某本书提到过... 不过没什么机会实践 C++ 很快就忘了为什么那本书提到shared_point= =||

hjack 发表于 2008-5-15 18:06

Shared_point??
I am interested. Share more,please?

iptton 发表于 2008-5-15 18:13

应该是 shared_ptr...

boost中定义的..


头文件: "boost/shared_ptr.hpp"

几乎所有稍微复杂点的程序都需要某种形式的引用计数智能指针。这些智能指针让我们不再需要为了控制被两个或多个对象共享的对象的生存期而编写复杂的逻辑。当引用计费降为零,没有对象再需要这个共享的对象,这个对象就自动被销毁了。引用计数智能指针可以分为插入式(intrusive)和非插入式(non-intrusive) 两类。前者要求它所管理的类提供明确的函数或数据成员用于管理引用计数。这意味着在类的设计时就必须预见到它将与一个插入式的引用计数智能指针一起工作,或者重新设计它。非插入式的引用计数智能指针对它所管理的类没有任何要求。引用计数智能指针拥有与它所存指针有关的内存的所有权。没有智能指针的帮助,对象的共享会存在问题,必须有人负负责删除共享的内存。谁负责?什么时候删除?没有智能指针,你必须在管理的内存之外增加生存期的管理,这意味着在各个拥有者之间存在更强的依赖关系。换言之,没有了重用性并增加了复杂性。

被管理的类可能拥有一些特性使得它更应该与引用计数智能指针一起使用。例如,它的复制操作很昂贵,或者它所代表的有些东西必须被多个实例共享,这些特性都值得去共享所有权。还有一种情形是共享的资源没有一个明确的拥有者。使用引用计数智能指针可以在需要访问共享资源的对象之间共享资源的所有权。引用计数智能指针还让你可以把对象指针存入标准库的容器中而不会有泄漏的风险,特别是在面对异常或要从容器中删除元素的时候。如果你把指针放入容器,你就可以获得多态的好处,可以提高性能(如果复制的代价很高的话),还可以通过把相同的对象放入多个辅助容器来进行特定的查找。


for more: http://www.google.cn/search?q=shared_ptr&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:zh-CN:unofficial&client=firefox-a

iptton 发表于 2008-5-15 18:20

googled容器+shared_ptr
it seems thatshared_ptr meets your requirement...

iptton 发表于 2008-5-15 18:22

shared_ptr解决了在多个指针间共享对象所有权的问题,它可以安全的放入标准容器,并且是线程安全的。

hjack 发表于 2008-5-15 18:30

Great.
页: [1] 2
查看完整版本: 类型转换问题