How to encapsulating Object?

Discussion in 'C++' started by Immortal_Nephi@hotmail.com, Sep 8, 2008.

  1. Guest

    Sometimes, programmers decide to create only one object. One object
    is used for a whole program. You can run two or more programs at the
    same time. Each program has a copy of its own object.
    They decide to use global variable and global function. The
    namespace is the only way to encapsulate global variable and global
    function. The source code is much easier to be readable if namespace
    is used. Multiple modules can share global varable and global
    function without any confusion.
    They can create dynamic linked library to include global variable and
    global functions. The clients can reuse library.
    If programmers decde to create more than one object on a whole
    program, class is the answer.
    Take a look at my example code. Please let me know if my source code
    looks good design. If only one object is ued, should I use global
    variables and globa functions or struct with global function?

    // Main.cpp

    namespace Color
    {
    unsigned int Red = 0;
    unsigned int Green = 0;
    unsigned int Blue = 0;

    unsigned int RGB = 0;

    void Modify_RGB()
    {
    Red += 10;
    Green += 10;
    Blue += 10;

    RGB = (Red << 8) | (Green << 4) | Blue;
    }

    struct Pixels
    {
    unsigned int Red;
    unsigned int Green;
    unsigned int Blue;
    };

    Pixels pixels; // Already defined in file scope.

    void Zero_RGB(Pixels &pixels)
    {
    pixels.Red = 0;
    pixels.Green = 0;
    pixels.Blue = 0;
    }
    }

    int main()
    {
    Color::Red = 25;
    Color::Green = 50;
    Color::Blue = 75;

    Color::Modify_RGB();

    Color::Zero_RGB(Color::pixels);

    return 0;
    }

    Nephi
     
    , Sep 8, 2008
    #1
    1. Advertising

  2. Noah Roberts Guest

    wrote:
    > Sometimes, programmers decide to create only one object. One object
    > is used for a whole program. You can run two or more programs at the
    > same time. Each program has a copy of its own object.
    > They decide to use global variable and global function. The
    > namespace is the only way to encapsulate global variable and global
    > function. The source code is much easier to be readable if namespace
    > is used. Multiple modules can share global varable and global
    > function without any confusion.
    > They can create dynamic linked library to include global variable and
    > global functions. The clients can reuse library.
    > If programmers decde to create more than one object on a whole
    > program, class is the answer.
    > Take a look at my example code. Please let me know if my source code
    > looks good design. If only one object is ued, should I use global
    > variables and globa functions or struct with global function?
    >


    singleton
     
    Noah Roberts, Sep 8, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Sep 8, 5:48 pm, Noah Roberts <> wrote:
    > wrote:
    > > Sometimes, programmers decide to create only one object. One object
    > > is used for a whole program. You can run two or more programs at the
    > > same time. Each program has a copy of its own object.
    > > They decide to use global variable and global function. The
    > > namespace is the only way to encapsulate global variable and global
    > > function. The source code is much easier to be readable if namespace
    > > is used. Multiple modules can share global varable and global
    > > function without any confusion.
    > > They can create dynamic linked library to include global variable and
    > > global functions. The clients can reuse library.
    > > If programmers decde to create more than one object on a whole
    > > program, class is the answer.
    > > Take a look at my example code. Please let me know if my source code
    > > looks good design. If only one object is ued, should I use global
    > > variables and globa functions or struct with global function?


    > singleton


    Maybe. His first sentence is "Sometimes, programmers decide to
    create only one object"; if the client programmer decides to
    create only one object, then he only creates one object, and
    that's it. A singleton is used if the abstraction of the class
    is such that it doesn't make sense to create more than one
    object (and that object should have the lifetime of the
    program).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 9, 2008
    #3
  4. a écrit :
    > Sometimes, programmers decide to create only one object. One object
    > is used for a whole program. You can run two or more programs at the
    > same time. Each program has a copy of its own object.
    > They decide to use global variable and global function.


    Do you mean functions that directly use the global ?
    I rather use the global as default parameters. That way, I can apply the
    same function locally:

    <from your example>
    void Modify_RGB(unsigned int& R=Red
    ,unsigned int& G=Green
    ,unsigned int& B=Blue)
    {...

    > The namespace is the only way to encapsulate global variable and global
    > function.


    You can also do it in a class.
    <from your example>
    struct Color
    {
    static unsigned int Red = 0;
    static unsigned int Green = 0;
    static unsigned int Blue = 0;

    static unsigned int RGB = 0;

    static void Modify_RGB()...

    This add a few restrictions: no alias with using and you cannot add
    extend the 'namespace' outside the point of definition.

    This can be useful if you want to ensure nobody will pollute your namespace.

    > The source code is much easier to be readable if namespace
    > is used.


    This has been debated and some don't use namespace because they think it
    adds complexity relatively to the scale of their project.

    And koenig lookup can yield oddities in some specific case.

    > Multiple modules can share global varable and global
    > function without any confusion.


    With your example, they might have some multithreading issues which is
    often the case nowadays.

    I don't see where the confusion would come. You can also initialize a
    module by providing it the context and not allow it to directly use the
    global; it is IMHO cleaner.

    > They can create dynamic linked library to include global variable and
    > global functions. The clients can reuse library.


    Globals/Singletons within a dynamically loaded library are more an added
    complexity than an asset; particularly when unloading.

    > If programmers decde to create more than one object on a whole
    > program, class is the answer.


    You can define a class and then use one instance as a global. Singletons
    are useful if you want to ensure there can be only one instance of the
    class but it is rarely necessary.

    --
    Michael
     
    Michael DOUBEZ, Sep 9, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phin
    Replies:
    1
    Views:
    4,785
  2. Greg Galloway

    Encapsulating HTML in XML using XSL

    Greg Galloway, Aug 5, 2004, in forum: XML
    Replies:
    1
    Views:
    438
    Martin Honnen
    Aug 5, 2004
  3. metaperl
    Replies:
    1
    Views:
    260
    Gabriel Genellina
    Dec 12, 2006
  4. John Goche

    class encapsulating reference

    John Goche, Nov 14, 2006, in forum: C++
    Replies:
    2
    Views:
    335
    John Goche
    Nov 20, 2006
  5. Marco
    Replies:
    7
    Views:
    367
    Marco
    Jun 23, 2008
Loading...

Share This Page