473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

class method static variable same across isntances?

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.
Oct 4 '07 #1
5 1740
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.
Oct 5 '07 #2
On Oct 5, 9:26 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
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++;
}

};
[snip]

Have you thought about deriving from a common base class?

E.g:

class Common
{
static std::vector<intEmptyData;

// ...

};

class Foo : public Common
{
// ...

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

--
Chris Val

Oct 5 '07 #3

"Chris ( Val )" <ch******@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Oct 5, 9:26 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>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++;
}

};

[snip]

Have you thought about deriving from a common base class?

E.g:

class Common
{
static std::vector<intEmptyData;

// ...

};

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_tEmptySet;
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_tIndicies_;
std::vector<PartIndexParts_;

};

My other option would be to use a try...catch block which I am not fond of
for using for something like this.
Oct 6 '07 #4
On Oct 6, 2:32 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Chris ( Val )" <chris...@gmail.comwrote in messagenews:11**********************@57g2000hsv.go oglegroups.com...


On Oct 5, 9:26 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
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++;
}
};
[snip]
Have you thought about deriving from a common base class?
E.g:
class Common
{
static std::vector<intEmptyData;
// ...
};
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
Oct 6 '07 #5
"Chris ( Val )" <ch******@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
On Oct 6, 2:32 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Chris ( Val )" <chris...@gmail.comwrote in
messagenews:11**********************@57g2000hsv.g ooglegroups.com...


On Oct 5, 9:26 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
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++;
}
>};
[snip]
Have you thought about deriving from a common base class?
E.g:
class Common
{
static std::vector<intEmptyData;
// ...
};
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.
Oct 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ruud de Jong | last post by:
I have the situation where I need to construct the name of a static method, and then retrieve the corresponding function from a class object. I thought I could just use __getattribute__ for this...
2
by: Victor Hannak | last post by:
I have a general performance question related to some code I am writing. Consider the following class: class FuncClass { public: FuncClass(unsigned short FunctionCode); ~FeuronClass(); double...
6
by: Anthony | last post by:
Hi, Can anyone help me out here? I need a construction where a static variable in a derived class is accessible through its base class. The base class is part of a framework. The variable in...
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
0
by: Ed West | last post by:
Hello, I am wondering about best practices for class hierarchies and using ADO.Net, especially the DataSet update/delete commands and the data relations... this needs to package/unpackage to xml...
4
by: Typpo | last post by:
Hi, This question is somewhat basic. Is it possible to call a method in frmMain, using another class? Here's a snippet: partial class frmMain : Form { private void MessageThis(String text)...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.