templates: despite "this->" is a call to independent function not executed
Dec 2, 2010 at 1:13pm UTC
Hello,
below you find code for a few template classes.
In DisconnectedState::connect() you find in line 85 where this->castSomeEvent of the base class should be called. This call silently does not take place. Instead the call is simply skipped.
Any idea why?
Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
class IHashable
{
public :
virtual ~IHashable(){}
virtual uintmax_t getHashCode() const {
return (uintmax_t) this ;
}
};
class IConnection : public IHashable
{
public :
virtual ~IConnection() {}
virtual void connect(){throw "IConnection.connect: override this method!" ;}
virtual void disconnect(){throw "IConnection.disconnect: override this method!" ;}
virtual int receive(){throw "IConnection.receive: override this method!" ;}
virtual void send(int value){throw "IConnection.send: override this method!" ;}
};
class Event : public IHashable
{
public :
Event(const char * name);
virtual ~Event();
string& getName();
private :
string _name;
};
class IEventSink
{
public :
IEventSink(){}
virtual ~IEventSink(){}
virtual void castEvent(const Event& event) = 0;
};
template <class T>
class StateBase
{
protected :
T& automaton;
IEventSink& eventSink;
public :
StateBase(T& aAutomaton, IEventSink& aEventSink) :
automaton(aAutomaton),
eventSink(aEventSink)
{
}
virtual ~StateBase(){}
virtual void castSomeEvent(const Event& event) //// this is the method that I hoped would be called!
{
eventSink.castEvent(event);
}
};
template <class T>
class DisconnectedState : public StateBase<T>, public IConnection
{
public :
static const Event CONNECT;
static const Event ERROR;
DisconnectedState(T& aAutomaton, IEventSink& aEventSink, Socket& aSocket)
: StateBase<T>(aAutomaton, aEventSink),
socket(aSocket)
{
}
virtual ~DisconnectedState(){}
virtual void connect()
{
try {
socket.connect();
} catch (...) {
this ->eventSink.castEvent(ERROR);
throw ;
}
this ->castSomeEvent(CONNECT); // <<< this call is not executed; instead jump to ***
}
virtual void disconnect(){}
virtual int receive()
{
throw new ConnectionClosedException("Can not receive: disconnected!" );
}
virtual void send(int value)
{
throw new ConnectionClosedException("Can not send: disconnected!" );
}
protected :
Socket& socket;
};
template <class AI>
const Event DisconnectedState<AI>::CONNECT("CONNECT" );
template <class AI>
const Event DisconnectedState<AI>::ERROR("ERROR" );
template <class AI>
class AutomatonBase : implements IEventSink
{
public :
AutomatonBase()
{
}
virtual ~AutomatonBase()
{
}
virtual void castEvent(const Event& event)
{
}
protected :
AI* pState;
};
class ConnectAutomaton : public AutomatonBase<IConnection>, public IConnection {
public :
virtual ~ConnectAutomaton()
{
delete pSocket;
}
static ConnectAutomaton& createAutomaton() {
ConnectAutomaton* pNew = new ConnectAutomaton();
return *pNew;
}
virtual void connect()
{
pState->connect(); // call of DisconnectedState::connect() works fine.
} // *** this is where we end up instead of calling the desired method at <<< above!
virtual void disconnect()
{
pState->disconnect();
}
virtual int receive()
{
return pState->receive();
}
virtual void send(int value)
{
pState->send(value);
}
private :
ConnectAutomaton() : AutomatonBase<IConnection>(), IConnection()
{
pSocket = new Socket();
pDisconnected = new DisconnectedState<IConnection>(*this , *this , *pSocket);
this ->pState = pDisconnected;
}
Socket* pSocket;
IConnection* pDisconnected;
};
main()
{
ConnectAutomaton* pConnectAutomaton;
ConnectAutomaton& ca = ConnectAutomaton::createAutomaton();
pConnectAutomaton = &ca;
pConnectAutomaton->connect();
}
Last edited on Dec 2, 2010 at 3:50pm UTC
Dec 2, 2010 at 1:46pm UTC
Hi! Could you edit your post to put the code within code blocks? That way you can refer to a specific line number that you are having problems with.
Last edited on Dec 2, 2010 at 1:51pm UTC
Dec 2, 2010 at 2:38pm UTC
Thanks! My guess would be that you are hitting that throw ;
call on line 83, at which point the function stops executing and looks for the catch handler?
Dec 2, 2010 at 3:13pm UTC
Thank you, Snarky!
The catch block is never executed. But commenting the try...catch()... out is the key!
Now line 85 calls the correct method. This behavior leaves me a bit puzzled nevertheless...
Topic archived. No new replies allowed.