Originally Posted by
algorism
I don't know what you mean by this. Have you tried running the code I posted? Are you saying it doesn't run for you? It runs for me.
Originally Posted by
algorism
I don't know what you mean by this. Have you tried running the code I posted? Are you saying it doesn't run for you? It runs for me.
What IDE did you use?
When I copy your bare code in one .cpp file, in Dev C++ it compiles and runs.
But when I put it separetly in headers and .cpp files, in Visual Studio I get the error.
Also I looked in the book "Design Patterns - Gang of four"
for explanation of a Design pattern
TCPConnection.h - CONTEXT
Code:
class TCPConnection
{
private:
TCPState* m_state;
public:
TCPConnection();
void ActiveOpen();
void PassiveOpen();
void Close();
void Send();
void Acknowledge();
void Synchronize();
void ProcessOctet(TCPOctetStream*);
private:
friend class TCPState;
void ChangeState(TCPState*);
}
TCPConnection.cpp
Code:
TCPConnection::TCPConnection()
{
m_state = TCPClosed::Instance();
}
void TCPConnection::ChangeState(TCPState* state)
{
m_state = state;
}
void TCPConnection::ActiveOpen()
{
m_state->ActiveOpen(this);
}
void TCPConnection::PassiveOpen()
{
m_state->Close(this);
}
void TCPConnection::Close()
{
m_state->Close(this);
}
void TCPConnection::Acknowledge()
{
m_state->Acknowledge(this);
}
void TCPConnection::Synchronize()
{
m_state->Synchronize(this);
}
TCPState.h
Code:
class TCPState
{
protected:
void ChangeState(TCPConnection*, TCPState*);
public:
virtual void Transmit(TCPConnection*, TCPOctetStream*);
virtual void ActiveOpen(TCPConnection*);
virtual void PassiveOpen(TCPConnection*);
virtual void Close(TCPConnection*);
virtual void Synchronize(TCPConnection*);
virtual void Acknowledge(TCPConnection*);
virtual void Send(TCPConnection*);
}
class TCPListen : public TCPState()
{
public:
static TCPState* Instance(); // How should this method be implemented?
void Send(TCPContext* t)
{
ChangeState(t, TCPEEstablished::Instance());
}
}
TCPState.cpp
Code:
void TCPState::Transmit(TCPConnection*, TCPOctetStream*){}
void TCPState::ActiveOpen(TCPConnection*){}
void TCPState::Close(TCPConnection*){}
void TCPState::Synchronize(TCPConnection){}
void TCPState::ChangeState(TCPConnection* t, TCPState* state)
{
t->ChangeState(state);
}
/* TCPClosed.h */
Code:
class TCPClosed : public TCPState
{
public:
static TCPState* Instance();
virtual void ActiveOpen(TCPConnection*);
virtual void PassiveOpen(TCPConnection*);
}
/* TCPClosed.cpp */
Code:
void TCPClosed::ActiveOpen(TCPConnection* t)
{
ChangeState(t, TCPEstablished::Instance());
}
void TCPClosed::PassiveOpen(TCPConnection* t)
{
ChangeState(t, TCPListen::Instance());
}
void TCPEstablished::Close(TCPConnection* t)
{
ChangeState(t, TCPListen::Instance());
}
void TCPEstablished::Transmit(TCPConnection* t, TCPOctetStream* o)
{
t->ProcessOctet(o);
}
void TCPListen::Send(TCPConnection* t)
{
}
In any class that implements I2CState such as TCPListen or TCPClosed,
Code:
class TCPListen : public TCPState()
there is a method
Code:
static TCPState* Instance();
Instance() method returns pointer to TCPState ,and since this method is a static,
it doesn't have this pointer, so I am not sure how should I return any pointer by this function?
Is every state a singletron?
How would that function look?