根据条件将基类类指针指向几个可能的派生类型指针之一 [英] cast base class pointer to one of several possible derived type pointer based on condition

查看:199
本文介绍了根据条件将基类类指针指向几个可能的派生类型指针之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类 B 和几个派生模板类 D< int> D< float> D< double> 等等(超过十个)



在我的程序中,我发现一个情况,我有一个B指针,我知道指向一个实例的D专业化。我也有一个唯一的键来标识派生类型。



所以,我想使用我的基类指针和唯一的类型键来调用正确的派生类方法。我实际上想在几个地方做到这一点,所以我提出的一个解决方案不仅是丑陋,而且麻烦。



有更好的方法来访问在C ++中有基指针和唯一键的派生类成员吗?
$ b

我不想/不能修改基类。 boost库是公平的游戏。



这是我的潜在代码,但我不喜欢复制这个到处需要访问一个派生成员函数/变量。所有这一切用于一个成员函数调用?

  B * bptr = ... 
std :: type_info * typekey = ...

if(typekey ==& typeid(D< float>)){
D< float> * dptr = static_cast< D< float> *>(bptr);
dptr-> derivedMember();
} else if(typekey ==& typeid(D< double>)){
D< float> * dptr = static_cast< D double> *>(bptr);
dptr-> derivedMember();
} else if(typekey ==& typeid(D< int>)){
D< float> * dptr = static_cast< D< int> *>(bptr);
dptr-> derivedMember();
}


解决方案

$ c> D< type> :: 方法具有相同的名称'DerivedMember',我假设基类B不声明这是虚拟的,你可以更改你的hierachy:

  class B {etc.}; 

template< class T>
D类:public B
{
};

到:

 code> class B {etc.}; 

class C:public B
{
virtual void derivedMember()= 0;
};


template< class T>
class D:public C
{
public:
void derivedMember(){etc.};
};

然后您可以:

  void f(C * c)/ *或取一个B *和do:C * c = static_cast  * / 
{
c-> derivedMember();
}


I have a base class B and several derived template classes D<int>, D<float>, D<double>, etc. (so more than ten)

In my program, I find a situation where I have a B pointer that I KNOW points to an instance one of the D specializations. I also have a unique key that identifies the derived type.

So, I want to call the correct derived class method using my base class pointer and the unique type key. I actually want to do this in several places, so the one solution I have come up with is not only ugly, but cumbersome.

Is there a better way to access the derived class's members having a base pointer and a unique key in C++?

I don't want to / can't modify the base class. boost library is fair game.

Here is my potential code, but I'm not happy with replicating this everywhere I need to access a derived member function/variable. All of this for one member function call?!!

B * bptr = ...
std::type_info * typekey = ...

if        (typekey == &typeid(D<float>) ) {
    D<float> * dptr = static_cast<D<float>*>(bptr);
    dptr->derivedMember();
} else if (typekey == &typeid(D<double>) ) {
    D<float> * dptr = static_cast<D<double>*>(bptr);
    dptr->derivedMember();
} else if (typekey == &typeid(D<int>) ) {
    D<float> * dptr = static_cast<D<int>*>(bptr);
    dptr->derivedMember();
} 

解决方案

If all the D<type>:: methods have the same name 'DerivedMember`, and I assume that the base class B does not declare this as virtual, can you change your hierachy from:

class B { etc. };

template<class T>
class D : public B
{
};

to:

class B { etc. };

class C : public B
{
 virtual void derivedMember() = 0;
};


template<class T>
class D : public C
{
public:
void derivedMember() { etc. };
};

Then you could have:

void f(C* c) /* or take a B* and do: C* c = static_cast<C*>(b); */
{    
  c->derivedMember();
}

这篇关于根据条件将基类类指针指向几个可能的派生类型指针之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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