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<intEmptyData;
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.