class method static variable same across isntances?

Discussion in 'C++' started by Jim Langston, Oct 5, 2007.

  1. Jim Langston

    Jim Langston Guest

    The output of the following program for me is:
    Same
    0 1 2

    Which is what I want. I just want to confirm this is well defined behavior,
    that a static local variable to a class function/method is the same instance
    across classes.

    #include <iostream>
    #include <vector>

    class Foo
    {
    public:
    std::vector<int>& Data( )
    {
    static std::vector<int> EmptyData;

    return EmptyData;
    }
    int Bar()
    {
    static int Val = 0;
    return Val++;
    }

    };

    int main()
    {
    Foo Bar1;
    Foo Bar2;
    std::vector<int>& D1 = Bar1.Data( );
    std::vector<int>& D2 = Bar2.Data( );

    if ( &D1 == &D2 )
    std::cout << "Same\n";
    else
    std::cout << "Different\n";

    std::cout << Bar1.Bar() << " ";
    std::cout << Bar2.Bar() << " ";
    std::cout << Bar1.Bar();
    return 0;
    }

    My actual usage will be to return an empty set in a tree like class if the
    part is not found, but within that class I want to check to see if it was
    found or not. I can see if it was found or not by comparing the pointers of
    the returned reference to the local static.
     
    Jim Langston, Oct 5, 2007
    #1
    1. Advertising

  2. Jim Langston

    Ian Collins Guest

    Jim Langston wrote:
    > The output of the following program for me is:
    > Same
    > 0 1 2
    >
    > Which is what I want. I just want to confirm this is well defined behavior,
    > that a static local variable to a class function/method is the same instance
    > across classes.
    >

    Yes, it is. Member functions are unique.

    --
    Ian Collins.
     
    Ian Collins, Oct 5, 2007
    #2
    1. Advertising

  3. On Oct 5, 9:26 am, "Jim Langston" <> wrote:
    > The output of the following program for me is:
    > Same
    > 0 1 2
    >
    > Which is what I want. I just want to confirm this is well defined behavior,
    > that a static local variable to a class function/method is the same instance
    > across classes.
    >
    > #include <iostream>
    > #include <vector>
    >
    > class Foo
    > {
    > public:
    > std::vector<int>& Data( )
    > {
    > static std::vector<int> EmptyData;
    >
    > return EmptyData;
    > }
    > int Bar()
    > {
    > static int Val = 0;
    > return Val++;
    > }
    >
    > };


    [snip]

    Have you thought about deriving from a common base class?

    E.g:

    class Common
    {
    static std::vector<int> EmptyData;

    // ...

    };

    class Foo : public Common
    {
    // ...

    If you want common functionality across all instances,
    it might be worth looking into something like this.

    --
    Chris Val
     
    Chris ( Val ), Oct 5, 2007
    #3
  4. Jim Langston

    Jim Langston Guest

    "Chris ( Val )" <> wrote in message
    news:...
    > On Oct 5, 9:26 am, "Jim Langston" <> wrote:
    >> The output of the following program for me is:
    >> Same
    >> 0 1 2
    >>
    >> Which is what I want. I just want to confirm this is well defined
    >> behavior,
    >> that a static local variable to a class function/method is the same
    >> instance
    >> across classes.
    >>
    >> #include <iostream>
    >> #include <vector>
    >>
    >> class Foo
    >> {
    >> public:
    >> std::vector<int>& Data( )
    >> {
    >> static std::vector<int> EmptyData;
    >>
    >> return EmptyData;
    >> }
    >> int Bar()
    >> {
    >> static int Val = 0;
    >> return Val++;
    >> }
    >>
    >> };

    >
    > [snip]
    >
    > Have you thought about deriving from a common base class?
    >
    > E.g:
    >
    > class Common
    > {
    > static std::vector<int> EmptyData;
    >
    > // ...
    >
    > };
    >
    > class Foo : public Common
    > {
    > // ...
    >
    > If you want common functionality across all instances,
    > it might be worth looking into something like this.


    It's actually used in a method to return a reference to a set using a form
    of recursion.

    class PartIndex
    {
    public:
    PartIndex( const std::string& Name = "Torso" ): Name_( Name ) {}
    // Lot of other public methods used to populate Indicies_ and Parts_
    std::set<size_t>& PartIndicies( const std::string& Name )
    {
    static std::set<size_t> EmptySet;
    EmptySet.clear();

    if ( Name_ == Name )
    {
    return PartIndicies();
    }
    else
    {
    for ( std::vector<PartIndex>::iterator it = Parts_.begin(); it
    != Parts_.end(); ++it )
    {
    std::set<size_t>& ReturnSet = (*it).PartIndicies( Name );
    if ( &ReturnSet != &EmptySet )
    return ReturnSet;
    }
    return EmptySet;
    }
    }
    private:
    std::string Name_;
    std::set<size_t> Indicies_;
    std::vector<PartIndex> Parts_;

    };

    My other option would be to use a try...catch block which I am not fond of
    for using for something like this.
     
    Jim Langston, Oct 6, 2007
    #4
  5. On Oct 6, 2:32 pm, "Jim Langston" <> wrote:
    > "Chris ( Val )" <> wrote in messagenews:...
    >
    >
    >
    >
    >
    > > On Oct 5, 9:26 am, "Jim Langston" <> wrote:
    > >> The output of the following program for me is:
    > >> Same
    > >> 0 1 2

    >
    > >> Which is what I want. I just want to confirm this is well defined
    > >> behavior,
    > >> that a static local variable to a class function/method is the same
    > >> instance
    > >> across classes.

    >
    > >> #include <iostream>
    > >> #include <vector>

    >
    > >> class Foo
    > >> {
    > >> public:
    > >> std::vector<int>& Data( )
    > >> {
    > >> static std::vector<int> EmptyData;

    >
    > >> return EmptyData;
    > >> }
    > >> int Bar()
    > >> {
    > >> static int Val = 0;
    > >> return Val++;
    > >> }

    >
    > >> };

    >
    > > [snip]

    >
    > > Have you thought about deriving from a common base class?

    >
    > > E.g:

    >
    > > class Common
    > > {
    > > static std::vector<int> EmptyData;

    >
    > > // ...

    >
    > > };

    >
    > > class Foo : public Common
    > > {
    > > // ...

    >
    > > If you want common functionality across all instances,
    > > it might be worth looking into something like this.

    >
    > It's actually used in a method to return a reference to a set using a form
    > of recursion.


    [snipped code]

    Ok, but how is the code you posted relevant to what I asked?

    You posted an interest in finding out about a static retaining
    its value across all instances of a class. I just wanted to put
    forward the suggestion of using a common base class for such
    things, to simplify your code and see what your thoughts were :)

    --
    Chris Val
     
    Chris ( Val ), Oct 6, 2007
    #5
  6. Jim Langston

    Jim Langston Guest

    "Chris ( Val )" <> wrote in message
    news:...
    > On Oct 6, 2:32 pm, "Jim Langston" <> wrote:
    >> "Chris ( Val )" <> wrote in
    >> messagenews:...
    >>
    >>
    >>
    >>
    >>
    >> > On Oct 5, 9:26 am, "Jim Langston" <> wrote:
    >> >> The output of the following program for me is:
    >> >> Same
    >> >> 0 1 2

    >>
    >> >> Which is what I want. I just want to confirm this is well defined
    >> >> behavior,
    >> >> that a static local variable to a class function/method is the same
    >> >> instance
    >> >> across classes.

    >>
    >> >> #include <iostream>
    >> >> #include <vector>

    >>
    >> >> class Foo
    >> >> {
    >> >> public:
    >> >> std::vector<int>& Data( )
    >> >> {
    >> >> static std::vector<int> EmptyData;

    >>
    >> >> return EmptyData;
    >> >> }
    >> >> int Bar()
    >> >> {
    >> >> static int Val = 0;
    >> >> return Val++;
    >> >> }

    >>
    >> >> };

    >>
    >> > [snip]

    >>
    >> > Have you thought about deriving from a common base class?

    >>
    >> > E.g:

    >>
    >> > class Common
    >> > {
    >> > static std::vector<int> EmptyData;

    >>
    >> > // ...

    >>
    >> > };

    >>
    >> > class Foo : public Common
    >> > {
    >> > // ...

    >>
    >> > If you want common functionality across all instances,
    >> > it might be worth looking into something like this.

    >>
    >> It's actually used in a method to return a reference to a set using a
    >> form
    >> of recursion.

    >
    > [snipped code]
    >
    > Ok, but how is the code you posted relevant to what I asked?
    >
    > You posted an interest in finding out about a static retaining
    > its value across all instances of a class. I just wanted to put
    > forward the suggestion of using a common base class for such
    > things, to simplify your code and see what your thoughts were :)


    Well, my code shows how I am using the fact that a static is the same
    instance across instances of the object. And how using a common base class
    wouldn't help me in the least.
     
    Jim Langston, Oct 6, 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. =?Utf-8?B?UHVuaXNoZXI=?=

    Need same viewstate for same page across multiple servers

    =?Utf-8?B?UHVuaXNoZXI=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    424
    John Timney \( MVP \)
    Mar 23, 2006
  2. astrorail
    Replies:
    4
    Views:
    598
    Tony Morris
    Oct 1, 2004
  3. Patrick Hoffmann
    Replies:
    3
    Views:
    2,850
    Christian Jan├čen
    Aug 8, 2003
  4. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,434
    Denis Perelyubskiy
    Sep 5, 2003
  5. Mug
    Replies:
    4
    Views:
    308
Loading...

Share This Page