Ryan's District
November 22, 2008, 04:12:41 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
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 - Managing single instance classes  (Read 3635 times)
0 Members and 3 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, 08:49:29 AM »

PROBLEM: robert@stud.unit.no (Robert Schmidt)

I find C++ to be of great value when it comes to modelling various
elements of a program, be it simulations of real life things, or more
abstract concepts like data structures.

I have one minor problem though.  I'm modelling the input devices of my
software in C++, and since there is only one Keyboard, one Mouse, one Gamecard
etc, I want to prohibit that several instances can be created of these
classes.  I see two possible solutions, but don't particularly fancy any
of them:

RESPONSE: rfries@netcom.com (Robert Fries)

I don't understand why you feel that creation of several instances of these
classes must be prohibited. Any instantiation would have to be done
explicitly. Just don't do it twice.

And I didn't understand what felt wrong about using global objects
for these devices. They are GLOBAL resources, available to, and needed
by, much of the program. This sounds like one of the valid
uses for global objects.

RESPONSE: vinoski@apollo.hp.com (Steve Vinoski), 16 Nov 93

Instead of thinking in terms of global variables, think of global
resources that can be accessed via local class instances:

void foo()
{
    Keyboard k;
    if (k.some_operation()) {
        ...
    }
    ...
}

The "globalness" of the keyboard is hidden in the Keyboard class via a
static pointer to the "real" Keyboard:

class RealKeyboard;   // can be hidden in implementation file

class Keyboard
{
  public:
    Keyboard() {if (kbd == 0) kbd = new RealKeyboard;}

    int some_operation() {return kbd->some_operation();}

    // details omitted

  private:
    static RealKeyboard *kbd;
};

(Given the forward decl of RealKeyboard, I realize that neither the
Keyboard ctor nor some_operation() would compile as shown; I only
wrote them this way for the sake of brevity.)

This arrangement ensures that a single instance of RealKeyboard gets
set up when required, and only a single instance is ever created
regardless of the number of instances of the Keyboard resource handle
class that are created to access it.  The RealKeyboard class can be
fully declared and defined within the implementation file to prevent
clients from using it directly (alternatively it could be forward
declared within the Keyboard class and fully declared and defined in
the impl file, if your compiler supports the new "forward declaration
of nested classes" feature (which cfront 3.0.2 appears to handle
correctly)).

This approach comes in quite handy for multi-threaded code since mutex
locking of global resources can be hidden inside the resource handle
classes, i.e., no need to deal with a global variable and a separate
yet related global mutex.

Naturally, there are other ways to write the resource handle class,
e.g., a reference-counting scheme.


[ We use the RealKeyboard method in handling translations of user
  keystrokes into editing meta-codes in ob and wb. -adc ]


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
« on: October 07, 2006, 08:49:29 AM »

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

Archive - WAP2 - WAP - imode
Sponsors: RAYAN.tv
-

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

Google visited last this page Yesterday at 11:17:34 AM