问题描述
如果以下代码符合标准,我希望得到一些反馈,并且
是否有明显的冗余或效率低下?代码的目的是
来模拟实现接口的结构FuBar的行为
InterfaceFuBar包含两个函数Fu和Bar。目标是这样做,而不是诉诸虚拟功能。
#include< iostream>
模板< typename SELF> ; struct ITABLE_InterfaceFuBar {
void(SELF :: * Fu)();
void(SELF :: * Bar)();
} ;
struct InterfaceFuBar {
struct DUMMY_InterfaceFuBar {
void Fu(){};
void Bar() {};
};
struct DUMMY_ITABLE_InterfaceFuBar:public
ITABLE_InterfaceFuBar< DUMMY_InterfaceFuBar> {};
DUMMY_InterfaceFuBar * mpObject;
DUMMY_ITABLE_InterfaceFuBar * mpTable;
InterfaceFuBar(void * pObject = NULL,void * pTable = NULL){
mpObject =(DUMMY_InterfaceFuBar *)pObject;
mpTable =(DUMMY_ITABLE_InterfaceFuBar *)pTable;
};
void Fu(){(mpObject-> *(mpTable-> Fu))(); };
void Bar(){(mpObject-> *(mpTable-> Bar))(); };
};
模板< typename I,typename M> struct Implementor {
operator I(){return I(NULL,NULL); };
};
模板< typename M> struct Implementor< InterfaceFuBar,M> {
运算符InterfaceFuBar(){
static ITABLE_InterfaceFuBar< M> tmp = {M :: Fu,M :: Bar};
返回InterfaceFuBar(this,(void *)& tmp);
};
};
struct FuBar:public Implementor< InterfaceFuBar,FuBar> {
void Fu(){std :: cout<< "福" <<的std :: ENDL; };
void Bar(){std :: cout<< "栏" <<的std :: ENDL; };
};
int main(){
FuBar f;
InterfaceFuBar i = f;
i.Fu();
std :: cin.get();
返回0;
}
-
Christopher Diggins
I would like some feedback if the follow code is standard compliant, and are
there any obvious redundancies or inefficiencies? The purpose of the code is
to emulate the behaviour of a struct FuBar which implements an interface
InterfaceFuBar which contains two functions Fu and Bar. The goal is to do so
without resorting to virtual functions.
#include <iostream>
template<typename SELF> struct ITABLE_InterfaceFuBar {
void(SELF::*Fu)();
void(SELF::*Bar)();
};
struct InterfaceFuBar {
struct DUMMY_InterfaceFuBar {
void Fu() { };
void Bar() { };
};
struct DUMMY_ITABLE_InterfaceFuBar : public
ITABLE_InterfaceFuBar<DUMMY_InterfaceFuBar> { };
DUMMY_InterfaceFuBar* mpObject;
DUMMY_ITABLE_InterfaceFuBar* mpTable;
InterfaceFuBar(void* pObject = NULL, void* pTable = NULL) {
mpObject = (DUMMY_InterfaceFuBar*)pObject;
mpTable = (DUMMY_ITABLE_InterfaceFuBar*)pTable;
};
void Fu() { (mpObject->*(mpTable->Fu))(); };
void Bar() { (mpObject->*(mpTable->Bar))(); };
};
template<typename I, typename M> struct Implementor {
operator I() { return I(NULL, NULL); };
};
template<typename M> struct Implementor<InterfaceFuBar, M> {
operator InterfaceFuBar() {
static ITABLE_InterfaceFuBar<M> tmp = { M::Fu, M::Bar };
return InterfaceFuBar(this, (void*)&tmp);
};
};
struct FuBar : public Implementor<InterfaceFuBar, FuBar> {
void Fu() { std::cout << "fu" << std::endl; };
void Bar() { std::cout << "bar" << std::endl; };
};
int main() {
FuBar f;
InterfaceFuBar i = f;
i.Fu();
std::cin.get();
return 0;
}
--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com
推荐答案
简短说明。 C风格的演员是邪恶的。改为使用C ++样式转换。
Short note. C style casts are evil. Use the C++ style casts instead.
[snip]
[snip]
你基本上是从头开始重建C ++的虚拟调度机制。这里的内容与您提供的代码
完全相同,但使用的是C ++现有的虚拟调度机制。你会发现
这个并不比上面的
代码中的手动双重间接更贵。
class InterfaceFuBar
{
struct IFB_base
{
virtual void Fu()= 0;
虚拟空格Bar()= 0;
};
模板< typename T>
struct IFB:public IFB_base
{
IFB(T& obj):m_obj(obj){}
虚拟void Fu()
{
m_obj.Fu();
}
虚拟无效酒吧()
{
m_obj.Bar();
}
T& m_obj;
};
IFB_base * m_pIFB;
InterfaceFuBar(); //禁止默认初始化
InterfaceFuBar(const InterfaceFuBar&); //禁止复制init
InterfaceFuBar& operator =(const InterfaceFuBar&); //不允许复制
public:
template< typename T>
InterfaceFuBar(T& obj)
:m_pIFB(新IFB< T>)
{
}
~InterfaceFuBar()
{
删除m_pIFB;
}
模板< typename T>
InterfaceFuBar& operator =(T& obj)
{
删除m_pIFB;
m_pIFB =新IFB< T> ;;
}
无福()
{
m_pIFB-> Fu();
}
void Bar()
{
m_pIFB-> Bar();
}
};
Claudio Puviani
PS:此代码仅供参考。我没有打扰编译或测试它,它不是例外安全等等。
You''re basically rebuilding C++''s virtual dispatch mechanism from scratch
inside InterfaceFuBar. Here''s something that does the same thing as the code
you provided, but using C++''s existing virtual dispatch mechanism. You''ll find
that this is no more expensive than the manual double indirection that''s in the
code above.
class InterfaceFuBar
{
struct IFB_base
{
virtual void Fu() = 0;
virtual void Bar() = 0;
};
template <typename T>
struct IFB : public IFB_base
{
IFB(T & obj) : m_obj(obj) {}
virtual void Fu()
{
m_obj.Fu();
}
virtual void Bar()
{
m_obj.Bar();
}
T & m_obj;
};
IFB_base * m_pIFB;
InterfaceFuBar(); // disallow default init
InterfaceFuBar(const InterfaceFuBar &); // disallow copy init
InterfaceFuBar & operator=(const InterfaceFuBar &); // disallow copy
public:
template <typename T>
InterfaceFuBar(T & obj)
: m_pIFB(new IFB<T>)
{
}
~InterfaceFuBar()
{
delete m_pIFB;
}
template <typename T>
InterfaceFuBar & operator=(T & obj)
{
delete m_pIFB;
m_pIFB = new IFB<T>;
}
void Fu()
{
m_pIFB->Fu();
}
void Bar()
{
m_pIFB->Bar();
}
};
Claudio Puviani
P.S.: This code is for illustration purposes only. I didn''t bother compiling or
testing it, it''s not exception-safe, etc.
这篇关于请求对C ++代码段的反馈(标准合规性,效率等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!