Ryan's District
September 07, 2008, 02:44:27 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length

There are currently users in chat
News: Ryan's District Lottery: Claim your ticket or check
Jackpot details  
 
   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 IV  (Read 2137 times)
0 Members and 1 Guest 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:48 AM »

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

The final part of this example is shown below:

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

class Engine {
    // ...
  public:
    void start ();
    void stop  ();
  private:
   
    class StartButton : public Button {
   void press ();
    } start_b;
    friend StartButton;

    class StopButton : public Button {
   void press ();
    } stop_b;
    friend StopButton;
};

void Engine :: StartButton :: press ()
{
    C_addr (Engine, start_b)->start ();
}

void Engine :: StopButton :: press ()
{
    C_addr (Engine, stop_b)->stop ();
}

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

This solution relies on the "offsetof" facility defined in stddef.h.
An appropriate define would be:

#define C_addr(C, M) ((C*)(((char*)this)-offsetof(C, M)))

"An important feature of this solution is that, unlike the previous
solution, the [...] members do not require explicit initialization
from the containing object".
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.163 seconds with 28 queries.

Google visited last this page September 04, 2008, 03:17:50 PM