4

Delegate

  1. #include "Application.h"
  2. #include "Window.h"
  3.  
  4. #include <iostream>
  5.  
  6. int main()
  7. {
  8.     using namespace std;
  9.  
  10.     Application app;
  11.     Window w;
  12.  
  13.     cout << "Moving window to (100, 200)" << endl;
  14.     w.moveTo( 100, 200);
  15.     cout << "Setting delegate..." << endl;
  16.     w.setDelegate( &app );
  17.     cout << "Moving window to (10, 20)" << endl;
  18.     w.moveTo( 10, 20 );
  19.     cout << "Clearing delegate..." << endl;
  20.     w.setDelegate( 0 );
  21.     cout << "Moving window to (50, 70)" << endl;
  22.     w.moveTo( 50, 70 );
  23.  
  24. }
  1. #if !defined( _WINDOW_H )
  2. #define _WINDOW_H
  3.  
  4. #include "WindowDelegate.h"
  5.  
  6. class WindowDelegate;
  7.  
  8. class Window
  9. {
  10. public:
  11.     Window( unsigned x=0, unsigned y=0 ) : _x( x ), _y( y ) { _delegate = 0; }
  12.  
  13.     unsigned getX() { return _x; };
  14.     unsigned getY() { return _y; };
  15.  
  16.     void moveTo( unsigned, unsigned );
  17.  
  18.     void setDelegate( WindowDelegate *wd ) { _delegate = wd; }
  19.  
  20. private:
  21.     unsigned _x, _y;
  22.     WindowDelegate *_delegate;
  23. };
  24.  
  25. #endif
  1. #include "Window.h"
  2.  
  3. void Window::moveTo( unsigned x, unsigned y )
  4. {
  5.     _x = x;
  6.     _y = y;
  7.     if ( _delegate )
  8.         _delegate->windowDidMove( this );
  9. }
  10.  
  11. #if defined( STANDALONE )
  12.  
  13. #include <iostream>
  14.  
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19. }
  20.  
  21. #endif
  1. #if !defined( _WINDOW_DELEGATE_H )
  2. #define _WINDOW_DELEGATE_H
  3.  
  4. class Window;
  5.  
  6. class WindowDelegate
  7. {
  8. public:
  9.     virtual void windowDidMove( Window * ) = 0;
  10. };
  11.  
  12. #endif
  1. #include "WindowDelegate.h"
  2.  
  3. #if defined( STANDALONE )
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. }
  12.  
  13. #endif
  1. #if !defined( _APPLICATION_H )
  2. #define _APPLICATION_H
  3.  
  4. #include "Window.h"
  5. #include "WindowDelegate.h"
  6.  
  7. class Application : public WindowDelegate
  8. {
  9. public:
  10.     void windowDidMove( Window * );
  11. };
  12.  
  13. #endif
  1. #include "Application.h"
  2.  
  3. #include <iostream>
  4.  
  5. void Application::windowDidMove( Window *w )
  6. {
  7.     std::cout << "\nWindow moved to (" << w->getX() << ", " << w->getY() << ")!!!\n" << std::endl;
  8. }
  1. TEST=tdel
  2.  
  3. TEST_SRCS=main.cpp
  4. TEST_OBJS=$(TEST_SRCS:.cpp=.o)
  5.  
  6. SRCS=Application.cpp Window.cpp WindowDelegate.cpp
  7. OBJS=$(SRCS:.cpp=.o)
  8.  
  9. CPP=g++
  10. CPPFLAGS=-g
  11.  
  12. all:    $(OBJS)
  13. test:   $(TEST)
  14.  
  15. $(TEST):    $(TEST_OBJS) $(OBJS)
  16.     $(CPP) $(CPPFLAGS) $(TEST_OBJS) $(OBJS) -o $@
  17.  
  18. .PHONY: clean wipe
  19.  
  20. clean:
  21.     rm -f *.o
  22.    
  23. wipe:   clean
  24.     rm -f $(TEST)
$ make test
g++ -g -c -o main.o main.cpp
g++ -g -c -o Application.o Application.cpp
g++ -g -c -o Window.o Window.cpp
g++ -g -c -o WindowDelegate.o WindowDelegate.cpp
g++ -g main.o Application.o Window.o WindowDelegate.o -o tdel
$ tdel
Moving window to (100, 200)
Setting delegate...
Moving window to (10, 20)

Window moved to (10, 20)!!!

Clearing delegate...
Moving window to (50, 70)

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].