c++中dynamic_cast的作用
#include<iostream>
#include<memory>
//Someone eles‘s code,eg.library
class IGlyph
{
virtual ~IGlyph(){}
virtual std::string Text() = 0;
virtual IIcon* Icon() = 0;
//...
};
class IWidgetSelector
{
public:
virtual ~IWidgetSelector(){}
virtual void AddItem(IGlyph*) = 0;
virtual IIcon *Selection() = 0;
};
//Your code
class MyItem:public IGlyph
{
public:
virtual std::string Text()
{
return this->text;
}
virtual IIcon* Icon()
{
return this->icon.get();
}
void Activate()
{
std::cout << "My Item Activated" << std::endl;
}
std::string text;
std::auto_ptr<IIcon> icon;
};
void SpiffyForm::OnDoubleClick(IWidgetSelector* ws)
{
IGlyph *gylph = ws->Selection();
MyItem *item = dynamic_cast<MyItem*>(gylph);
if (item)
item->Activate();
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2025-01-04 05:09:17