Use of static keyword instead of global object

Discussion in 'C++' started by Sam.Gundry@gmail.com, Feb 13, 2007.

  1. Guest

    Hi,

    I wish to share an object through a bunch of functions without
    declaring it globally. I have achieved this through the following
    function:

    VideoReceiver* getReceiver()
    {
    static VideoReceiver *vr = new VideoReceiver();
    return vr;
    }

    So, the first time it is called a new VideoReceiver object is created.
    Subsequent calls return this object (without creating new ones).

    E.g, All one needs to do to access the VideoReceiver is:

    function blah()
    {
    VideoReceiver vr = getReceiver();
    vr->update()

    //do blah
    }

    Are there any problems or issues I should be aware of using this type
    of code? Is it a no-no? I make multiple calls to getReceiver() each
    frame.

    It was originally a hack to get going but now that I've advanced a
    reasonable way I'm considering just leaving it, since it is currently
    working as expected.

    Thanks for any advice or suggestions,
    Sam.
     
    , Feb 13, 2007
    #1
    1. Advertising

  2. Andre Kostur Guest

    "" <> wrote in
    news::

    > Hi,
    >
    > I wish to share an object through a bunch of functions without
    > declaring it globally. I have achieved this through the following
    > function:
    >
    > VideoReceiver* getReceiver()
    > {
    > static VideoReceiver *vr = new VideoReceiver();
    > return vr;
    > }


    Why dynamically allocate it? Why not simply make it a static local
    variable?

    > So, the first time it is called a new VideoReceiver object is created.
    > Subsequent calls return this object (without creating new ones).
    >
    > E.g, All one needs to do to access the VideoReceiver is:
    >
    > function blah()
    > {
    > VideoReceiver vr = getReceiver();


    I assume you meant "VideoReceiver * vr"....

    > vr->update()
    >
    > //do blah
    > }
    >
    > Are there any problems or issues I should be aware of using this type
    > of code? Is it a no-no? I make multiple calls to getReceiver() each
    > frame.


    I don't think that the object will ever be destroyed. With a local
    static it will (sometime after main....)

    > It was originally a hack to get going but now that I've advanced a
    > reasonable way I'm considering just leaving it, since it is currently
    > working as expected.
    >
    > Thanks for any advice or suggestions,


    Perhaps make it a static local variable and return a reference to it.
     
    Andre Kostur, Feb 13, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 12, 7:54 pm, "" <>
    wrote:
    > Hi,
    >
    > I wish to share an object through a bunch of functions without
    > declaring it globally. I have achieved this through the following
    > function:
    >
    > VideoReceiver* getReceiver()
    > {
    > static VideoReceiver *vr = new VideoReceiver();
    > return vr;
    >
    > }
    >
    > So, the first time it is called a new VideoReceiver object is created.
    > Subsequent calls return this object (without creating new ones).
    >
    > E.g, All one needs to do to access the VideoReceiver is:
    >
    > function blah()
    > {
    > VideoReceiver vr = getReceiver();
    > vr->update()
    >
    > //do blah
    >
    > }
    >
    > Are there any problems or issues I should be aware of using this type
    > of code? Is it a no-no? I make multiple calls to getReceiver() each
    > frame.
    >
    > It was originally a hack to get going but now that I've advanced a
    > reasonable way I'm considering just leaving it, since it is currently
    > working as expected.
    >
    > Thanks for any advice or suggestions,
    > Sam.


    The function is fine except I would return
    a reference to the object - this way there
    is no chance of anyone deleting the object
    by accident.

    Also, as another poster said, there's no
    need for dynamic allocation - just use a
    static local.

    But for multi-threaded programs, the construction
    of the object is not safe. See, for instance,
    Modern C++ Design for a good discussion on this.

    Some times, this is ok. For instance, I use a
    static local log object inside a function. But
    I make sure it is used (and hence constructed)
    in the main thread before any other threads are
    invoked.
     
    , Feb 13, 2007
    #3
  4. On 12 Feb 2007 16:54:54 -0800, "" <>
    wrote:

    >Hi,
    >
    >I wish to share an object through a bunch of functions without
    >declaring it globally. I have achieved this through the following
    >function:
    >
    >VideoReceiver* getReceiver()
    >{
    > static VideoReceiver *vr = new VideoReceiver();
    > return vr;
    >}
    >
    >So, the first time it is called a new VideoReceiver object is created.
    >Subsequent calls return this object (without creating new ones).
    >
    >E.g, All one needs to do to access the VideoReceiver is:
    >
    >function blah()
    >{
    > VideoReceiver vr = getReceiver();
    > vr->update()
    >
    > //do blah
    >}
    >
    >Are there any problems or issues I should be aware of using this type
    >of code? Is it a no-no? I make multiple calls to getReceiver() each
    >frame.
    >
    >It was originally a hack to get going but now that I've advanced a
    >reasonable way I'm considering just leaving it, since it is currently
    >working as expected.
    >
    >Thanks for any advice or suggestions,
    >Sam.


    There is no problem with this kind of code, except that the VideoReceiver
    object is never destroyed.

    Your solution is actually a common implementation of the Singleton pattern in
    C++. A more typical code would be:


    MyClass& getClassInstance()
    {
    MyClass myClass;
    return myClass;
    }


    The pattern you are using also solves a common problems with globals:
    initialization order.

    -dr
     
    Dave Rahardja, Feb 13, 2007
    #4
  5. Guest

    On Feb 12, 9:16 pm, Dave Rahardja
    <> wrote:
    > On 12 Feb 2007 16:54:54 -0800, "" <>
    > wrote:
    >
    >
    >
    > >Hi,

    >
    > >I wish to share an object through a bunch of functions without
    > >declaring it globally. I have achieved this through the following
    > >function:

    >
    > >VideoReceiver* getReceiver()
    > >{
    > > static VideoReceiver *vr = new VideoReceiver();
    > > return vr;
    > >}

    >
    > >So, the first time it is called a new VideoReceiver object is created.
    > >Subsequent calls return this object (without creating new ones).

    >
    > >E.g, All one needs to do to access the VideoReceiver is:

    >
    > >function blah()
    > >{
    > > VideoReceiver vr = getReceiver();
    > > vr->update()

    >
    > > //do blah
    > >}

    >
    > >Are there any problems or issues I should be aware of using this type
    > >of code? Is it a no-no? I make multiple calls to getReceiver() each
    > >frame.

    >
    > >It was originally a hack to get going but now that I've advanced a
    > >reasonable way I'm considering just leaving it, since it is currently
    > >working as expected.

    >
    > >Thanks for any advice or suggestions,
    > >Sam.

    >
    > There is no problem with this kind of code, except that the VideoReceiver
    > object is never destroyed.
    >
    > Your solution is actually a common implementation of the Singleton pattern in
    > C++. A more typical code would be:
    >
    > MyClass& getClassInstance()
    > {
    > MyClass myClass;
    > return myClass;
    >
    > }
    >
    > The pattern you are using also solves a common problems with globals:
    > initialization order.
    >
    > -dr



    Like dr says, this is the singleton pattern and is quite useful. I
    find it useful to use a singleton as a non-copyable as well like so:

    class Singleton
    {
    public:
    virtual ~Singleton() {}

    protected:

    Singleton() {}

    Singleton( const Singleton& singleton ) {}

    Singleton&
    operator=( const Singleton& singleton ) {}
    } ;

    class MyClass : public Singleton
    {
    public:
    static MyClass&
    Instance()
    {
    if( !_instance )
    {
    boost::shared_ptr< MyClass > temp( new MyClass() ) ;
    _instance = temp ;
    }

    return _instnace ;
    }

    void
    foo() ;

    private:

    static boost::shared_ptr< MyClass > _instance ;
    } ;


    And then you can just do:

    MyClass::Instance().foo() ;

    or

    MyClass& inst = MyClass::Instance() ;
    inst.foo() ;

    To use it in code.

    HTH,
    Paul Davis
     
    , Feb 13, 2007
    #5
  6. Guest

    On Feb 13, 3:58 am, ""
    <> wrote:
    > On Feb 12, 9:16 pm, Dave Rahardja
    >
    >
    >
    > <> wrote:
    > > On 12 Feb 2007 16:54:54 -0800, "" <>
    > > wrote:

    >
    > > >Hi,

    >
    > > >I wish to share an object through a bunch of functions without
    > > >declaring it globally. I have achieved this through the following
    > > >function:

    >
    > > >VideoReceiver* getReceiver()
    > > >{
    > > > static VideoReceiver *vr = new VideoReceiver();
    > > > return vr;
    > > >}

    >
    > > >So, the first time it is called a new VideoReceiver object is created.
    > > >Subsequent calls return this object (without creating new ones).

    >
    > > >E.g, All one needs to do to access the VideoReceiver is:

    >
    > > >function blah()
    > > >{
    > > > VideoReceiver vr = getReceiver();
    > > > vr->update()

    >
    > > > //do blah
    > > >}

    >
    > > >Are there any problems or issues I should be aware of using this type
    > > >of code? Is it a no-no? I make multiple calls to getReceiver() each
    > > >frame.

    >
    > > >It was originally a hack to get going but now that I've advanced a
    > > >reasonable way I'm considering just leaving it, since it is currently
    > > >working as expected.

    >
    > > >Thanks for any advice or suggestions,
    > > >Sam.

    >
    > > There is no problem with this kind of code, except that the VideoReceiver
    > > object is never destroyed.

    >
    > > Your solution is actually a common implementation of the Singleton pattern in
    > > C++. A more typical code would be:

    >
    > > MyClass& getClassInstance()
    > > {
    > > MyClass myClass;
    > > return myClass;

    >
    > > }

    >
    > > The pattern you are using also solves a common problems with globals:
    > > initialization order.

    >
    > > -dr

    >
    > Like dr says, this is the singleton pattern and is quite useful. I
    > find it useful to use a singleton as a non-copyable as well like so:
    >
    > class Singleton
    > {
    > public:
    > virtual ~Singleton() {}
    >
    > protected:
    >
    > Singleton() {}
    >
    > Singleton( const Singleton& singleton ) {}
    >
    > Singleton&
    > operator=( const Singleton& singleton ) {}
    >
    > } ;
    >
    > class MyClass : public Singleton
    > {
    > public:
    > static MyClass&
    > Instance()
    > {
    > if( !_instance )
    > {
    > boost::shared_ptr< MyClass > temp( new MyClass() ) ;
    > _instance = temp ;
    > }
    >
    > return _instnace ;
    > }
    >
    > void
    > foo() ;
    >
    > private:
    >
    > static boost::shared_ptr< MyClass > _instance ;
    >
    > } ;
    >
    > And then you can just do:
    >
    > MyClass::Instance().foo() ;
    >
    > or
    >
    > MyClass& inst = MyClass::Instance() ;
    > inst.foo() ;
    >
    > To use it in code.
    >
    > HTH,
    > Paul Davis



    Totally looked this over after I posted and realized I messed up a
    couple points.

    in Instance():

    should be:

    return *( _instance.get() ) ;

    And don't forget to put:

    boost::shared_ptr< MyClass > MyClass::_instance, in a source file
    somewhere.

    Sorry if thats just confusing.

    Paul Davis
     
    , Feb 13, 2007
    #6
    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. tropos
    Replies:
    3
    Views:
    465
  2. Replies:
    32
    Views:
    877
    Ben Pfaff
    Mar 7, 2007
  3. Replies:
    6
    Views:
    458
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    355
  5. Alex Vinokur
    Replies:
    3
    Views:
    502
    Zoltan Juhasz
    Jun 15, 2012
Loading...

Share This Page