Jump to content

  • Log In with Google      Sign In   
  • Create Account


How does State Pattern work under C++?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Greatwolf   Members   -  Reputation: 122

Like
0Likes
Like

Posted 10 November 2007 - 08:50 PM

Hi guys, I'm using a state pattern for parsing a stream of bytes from a serial port and I'm running into a bit of problem implementing it. It seems that C++ doesn't count subclass as friends when you declare the base class as a friend. Now I've just checked the GoF Design patterns and it seems the sample code they show for the TCPconnection just makes the TCPState as friend and all the subclasses can work with TCPconnection's private members. But when I try to do this the compiler splits at me with an error, eg. member x is not accessible from class whatever. Seems the only way I could get this to work is to make each of the subclassed states a friend too. What am I missing here? Thanks


--{You fight like a dairy farmer!}

Sponsor:

#2 ToohrVyk   Members   -  Reputation: 1591

Like
1Likes
Like

Posted 10 November 2007 - 09:09 PM

Place inline protected methods in the base class, which forward your calls to the underlying object's private functions (it's possible, because the base class is a friend). Then, your derived classes can use the protected functions. If you prefer: the base class is responsible of interacting with the underlying object, and the derived classes are responsible for controlling that interaction.

#3 Neon2302   Members   -  Reputation: 122

Like
0Likes
Like

Posted 10 November 2007 - 10:43 PM

sorry I can't answer your question.
But if you don't mind. can I ask you how to user state pattern for parsing a stream.

I'm the one who interest in design pattern topic. but I'm not too good to understand by myself.

can you explain this to me?


thank you
Chet Chetchaiyan




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS