Ryan's District
August 29, 2008, 11:24:08 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length

There are currently users in chat
News: You are not allowed to view links.
Register or Login
How to Unlock Nokia mobile Phones

You are not allowed to view links.
Register or Login
Request any TV Show / series / Episodes / movie and we will get it for you for free

 
   Home   Help Search Chat Calendar Chess Shop Login Register  
Digg This!
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: C++ tips - Callbacks and multiple inheritance, part III  (Read 4501 times)
0 Members and 2 Guests are viewing this topic.
ben2ong2
Quality Poster
Paid
Hero Member
*****

Reputation: 17
Offline Offline

Gender: Male
Posts: 2374
9976.80 RD$

View Inventory
Send Money to ben2ong2

View Profile Awards
« on: October 07, 2006, 03:02:20 AM »

This example is based upon "Subobject Members", Stephen Dewhurst,
C++ Report, V5 N3. This is part 3 of a multipart example.

In this example, we assume the engine with two buttons as before.
Rather than using inheritance to handle the relationships between
the buttons and the engine, we will use "has-a". The buttons will
have knowledge of the engine that they are embedded within.

.................................................................

class Engine {
    // ...
  public:
    Engine ();
    void start ();
    void stop  ();
  private:
   
    class EngineButton : public Button {
   Engine &e;
   void (Engine::*f) ();
   EngineButton (Engine& er, void (Engine::*fp)())
       : e(er), f(fp) {}
   void press () { (e.*f) (); }
   friend Engine;
    };

    EngineButton start_b;
    EngineButton stop_b;
};


Engine :: Engine () : start_b(*this, start), stop_b(*this, stop) {}

.................................................................

This solution does not pollute the global namespace since EngineButton
is defined within the Engine class. Also the Engine class knows
exactly how many buttons it has (and can initialize them properly).
Logged

You are not allowed to view links.
Register or Login
Free Paid Survey & Home Business Resources.

You are not allowed to view links.
Register or Login
Free Article Directory | Quality Content
Ryan's District
   

 Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Archive - WAP2 - WAP - imode
Sponsors: TOP SEO Directory - Online Job Maker - List of Advertisers - BIG VENUE

Powered by MySQL Powered by PHP Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.186 seconds with 28 queries.

Google visited last this page Yesterday at 04:11:32 AM