gpt4 book ai didi

C++ 奇怪的重复模板模式,语法错误

转载 作者:行者123 更新时间:2023-11-28 06:01:16 25 4
gpt4 key购买 nike

我正在尝试编写一个模板事件类,并在这个基类中保留尽可能多的代码。我正在使用奇怪的重复模板模式,但我不确定我在这里做什么。

template< class EventType >
class Event
{
protected:

std::vector< EventType::Listener * > m_aListeners;

public:

void operator += ( EventType::Listener * pListener )
{
m_aListeners.push_back( pListener );
}

void operator -= ( EventType::Listener * pListener )
{
std::vector< EventType::Listener * >::reverse_iterator revIter = m_aListeners.rbegin();
for( ; revIter != m_aListeners.rend(); ++revIter )
if( revIter == pListener )
{
m_aListeners.remove( revIter );
break;
}
}

void Trigger( EventType::Data * pData )
{
std::vector< EventType::Listener * >::iterator iter = m_aListeners.begin();
for( ; iter != m_aListeners.end(); ++iter )
CallListenert( iter, pData );
}

virtual void CallListener( EventType::Listener * pListener, EventType::Data * pData ) = 0;
};

及其给定事件类型的子类:

class ConnectionSuccessEvent : public Event< ConnectionSuccessEvent >
{
public:

class Data
{
public:
Data( int iVal ) : m_iVal( iVal ) { }
public:
const int m_iVal;
};

class Listener
{
public:
virtual ~Listener() { }
virtual void OnConnectionSuccess( Data * pEventData ) = 0;
};

void CallListener( Listener * pListener, Data * pData )
{
pListener->OnConnectionSuccess( pData );
}
};

还有一个实现监听器类的类:

class MyClass :  public ConnectionSuccessEvent::Listener
{
public:

void OnConnectionSuccess( ConnectionSuccessEvent::Data * pEventData )
{
std::cout << "OnConnectionSuccess : " << pEventData->m_iVal << std::endl;
}
};

我使用如下:

MyClass oMyClassInstance;

ConnectionSuccessEvent oOnConnectionSuccess;

oOnConnectionSuccess += & oMyClassInstance;
oOnConnectionSuccess += & oMyClassInstance;

ConnectionSuccessEvent::Data oData( 456 );
oOnConnectionSuccess.Trigger( & oData );

oOnConnectionSuccess -= & oMyClassInstance;
oOnConnectionSuccess -= & oMyClassInstance;

这导致了几个编译错误,第一个是:

错误 2 error C2059: syntax error : '>' c:\dev\eventtest\event.h 16

这对应于 vector m_aListeners 的声明。

我有两个问题:

  • 是什么导致了我的错误?我是否不允许在我的类 Event 中使用 EventType::Listener ?

  • 奇怪的重复模式是如何可能的?我的意思是,要定义,派生类需要定义其父类。但是因为它是父类,它的父类需要定义它的模板参数类,即它是基类),那么这是一个问题,因为要定义一个,它需要先定义另一个。这就像在类 B 中有一个 A 的实例,在类 A 中有一个实例 ob B。

谢谢!

编辑:我不能使用 C++11。

最佳答案

我找到了一种可以接受的丑陋方式来做到这一点!

包含事件对象行为的基本事件类。

template< class EventListenerType, class EventDataType >
class BaseEvent
{
private:

std::vector< EventListenerType * > m_aListeners;

public:

void operator += ( EventListenerType * pListener )
{
m_aListeners.push_back( pListener );
}

void operator -= ( EventListenerType * pListener )
{
// When nI is 0 and gets decremented (i.e would be < 0), it "underflows" and
// becomes >= m_aListeners.size(), so we can detect that the last element (i.e
// the first in position ) has been treated when nI >= m_aListeners.size().

for( size_t nI = m_aListeners.size() - 1; nI <= m_aListeners.size(); --nI )
{
if( m_aListeners[ nI ] == pListener )
{
m_aListeners.erase( m_aListeners.begin() + nI );
break;
}
}
}

void Trigger( EventDataType * pData )
{
for( size_t nI = 0; nI < m_aListeners.size(); ++nI )
m_aListeners[ nI ]->OnEvent( pData );
}
};

和:

template< class EventDataType >
class EventListener
{
public:

virtual ~EventListener() { }

virtual void OnEvent( const EventDataType * pData ) = 0;
};

ConnectionSuccessEvent 特定的东西:

class ConnectionSuccessEventData
{
public:
const int m_iVal;
ConnectionSuccessEventData( int iVal ) : m_iVal( iVal ) { }
};

typedef EventListener< ConnectionSuccessEventData > ConnectionSuccessEventListener;
typedef BaseEvent< ConnectionSuccessEventListener, ConnectionSuccessEventData > ConnectionSuccessEvent;

DataReceivedEvent 特定的东西:

class DataReceivedEventData
{
public:
const float m_fVal;
DataReceivedEventData( float fVal ) : m_fVal( fVal ) { }
};

typedef EventListener< DataReceivedEventData > DataReceivedEventListener;
typedef BaseEvent< DataReceivedEventListener, DataReceivedEventData > DataReceivedEvent;

监听类:

class MyClass : public ConnectionSuccessEventListener, public DataReceivedEventListener
{
public:

void OnEvent( const ConnectionSuccessEventData * pData )
{
std::cout << "Connection success event : " << pData->m_iVal << std::endl;
}

void OnEvent( const DataReceivedEventData * pData )
{
std::cout << "Data received event : " << pData->m_fVal << std::endl;
}
};

用法:

MyClass oMyClassInstance;

ConnectionSuccessEvent oOnConnectionSuccess;
DataReceivedEvent oOnDataReceived;

oOnConnectionSuccess += & oMyClassInstance;
oOnDataReceived += & oMyClassInstance;
oOnConnectionSuccess += & oMyClassInstance;
oOnDataReceived += & oMyClassInstance;

ConnectionSuccessEventData oConnectionSuccessData( 123 );
oOnConnectionSuccess.Trigger( & oConnectionSuccessData );

DataReceivedEventData oDataReceivedData( 0.0f );
oOnDataReceived.Trigger( & oDataReceivedData );

oOnDataReceived += & oMyClassInstance;
oOnConnectionSuccess -= & oMyClassInstance;
oOnDataReceived += & oMyClassInstance;
oOnConnectionSuccess -= & oMyClassInstance;

关于C++ 奇怪的重复模板模式,语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33258527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com