Archived

This topic is now archived and is closed to further replies.

try_catch_this

Visitor Pattern?

Recommended Posts

I have a base class which has methods I cannot / do not want to change directly. But I would like to "add" to them as they are inhereted.
class C_Base
{
   public:
      C_Base( void );
      virtual ~C_base( void )
         {   }
      // Handles key ups

      LRESULT OnBaseKeyUp( C_Base &Base, WPARAM wParam, LPARAM               lParam )
      { 
         ...         
      }
};

C_OFBase: public: Base
{
   C_OFBase( void );

   // OnOFBaseKeyUp will now be registereg to accept WM_KEYUP

   LRESULT OnOFBaseKeyUp( C_OFBase &OFBase, WPARAM wParam, LPARAM lParam )
   {
      ... // Do what I want here

      return OnBaseKeyUp( OFBase, wParam, lParam );
   }   
};
I register OnOFBaseKeyUp as the message handler for WM_KEYUP and still get to call OnBaseKeyUp without any hassel. It is as if I did not change the orignal code. I cant always do this inorder to add to a method without changing it. 1) The function in the base class must remain essentially the same 2) but I want to be able to call another function after or before the base class function call. I was thinking about doing it with function pointers at the end of the base class method but someone from the IRC said that might be a bad idea. I have heard my problem can be solved through the "visitor pattern" and so far it has been a little difficult to grasp. Can anyone help. [edited by - try_catch_this on October 21, 2003 3:08:09 AM] [edited by - try_catch_this on October 21, 2003 3:09:21 AM]

Share this post


Link to post
Share on other sites
You don''t need any fancy-pants patterns here. (Especially the Visitor pattern, which is IMHO the stupidest pattern around).

Just keep in mind that virtual functions can be overloaded, and that base class functions can be explicitly invoked:


class CBase
{
public:
virtual ~CBase();
virtual void OnEvent() { std::cout << "In CBase::OnEvent\n"; }
};

class CDerived : public CBase
{
public:
virtual ~CDerived();
virtual void OnEvent()
{
std::cout << "Beginning CDerived::OnEvent\n";
CBase::OnEvent();
std::cout << "Ending CDerived::OnEvent\n";
}
};

int main()
{
CBase* foo = new CDerived;
foo->OnEvent();
}



How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
thanks for the reply

I got a related question


class C_Base
{
LERSULT static Afunc( C_Base&, WPARAM, LPARAM);
};

class C_Derived
{
LERSULT static Afunc( C_Base&, WPARAM, LPARAM);
};



How do you make sure C_Derived::Afunc is called instead of C_Base::Afunc?

C_Base::Afunc cant be virtual and static at the same time.

Share this post


Link to post
Share on other sites
quote:
Original post by try_catch_this
... COme on ppl ... A function cant be static and virtual at the same time. How do I get around this.


[I hope I don''t get banned]
Anyway, first of all it''d be nice to know what language you are using. I''m assuming C++, from the source code.

Virtual and static functions do not make sense, since static functions are "static" once you write a function, it''ll be the same function among all the "objects" created from the class.

I''m trying not to sound like an ass here, but I do think you have the two concepts mixed.

Share this post


Link to post
Share on other sites