473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can a Static member function know all instances?

SJ
Hi:
I have a class which has a static member function. The function
implements something common to all instances.
How can the static member function know all of the (Get access to the
instances' handles) instances?

Thanks in advance for any help
Jul 22 '05 #1
8 1818
SJ wrote:
I have a class which has a static member function. The function
implements something common to all instances.
How can the static member function know all of the (Get access to the
instances' handles) instances?


In the constructor of your class, have each instance register itself in
a static table.
Jul 22 '05 #2
SJ wrote:
I have a class which has a static member function. The function
implements something common to all instances.
How can the static member function know all of the (Get access to the
instances' handles) instances?


The usual implementation is a container of instance pointers as a static
data member of a class. Make sure you add to that list in every
constructor (thus you will have to re-implement all implicit ones), and
remove from that list in the destructor.

V
Jul 22 '05 #3

"SJ" <np****@hotmail .com> wrote in message
news:81******** *************** ***@posting.goo gle.com...
Hi:
I have a class which has a static member function. The function
implements something common to all instances.
How can the static member function know all of the (Get access to the
instances' handles) instances?

Thanks in advance for any help


Something like this?

class X
{
X() { instances.inser t(this); }
X(const X& rhs) { instances.inser t(this); ... }
// all other ctors similarly

~X() { instances.erase (this); }

static std::set<X*> instances;
static void some_func()
{
for (std::set<X*>:: const_iterator i = instance.begin( ); i !=
instances.end() ; ++i)
{
X* inst = *i;
// do something with inst
}
}
};

A hash table would probably be a better structure than a std::set.

One issue that occurs to me is that the compiler is allowed to optimise away
a copy constructor even if that copy constructor has a side effect. Not sure
if that is an issue here since I can't recall the circumstances in which
this is allowed to happen.

john
Jul 22 '05 #4

"SJ" <np****@hotmail .com> wrote in message
news:81******** *************** ***@posting.goo gle.com...
Hi:
I have a class which has a static member function. The function
implements something common to all instances.
How can the static member function know all of the (Get access to the
instances' handles) instances?

Thanks in advance for any help


It can't. Not directly, anyway. Static member functions can only access
static member data.

If you're doing something common to all instances, then that should probably
be done by manipulating static member data. Static member data is located
in one place only, not in every instance of the class, so there should be no
need to gain access to all existing instances. They all share the same
static data.

If what you're doing is manipulating some common data value, upon which
individual instances then make *other* calculations (to their own,
non-static, member data), then one solution is to use accessor functions in
the class for the non-static member data. Then, when you ask for one of
those calculated values, you can actually calculate it at that time from the
static data that the static function previously changed.

Or, if there is some reason that you *really* need to access all existing
instances of a class from a static function, then you'll need to somehow
register each instance with a container of some sort, and iterate through
the container to access those instances.

-Howard
Jul 22 '05 #5
"John Harrison" <jo************ *@hotmail.com> wrote in
news:2h******** ****@uni-berlin.de:

(...)
One issue that occurs to me is that the compiler is allowed to
optimise away a copy constructor even if that copy constructor has a
side effect. Not sure if that is an issue here since I can't recall
the circumstances in which this is allowed to happen.


In case of direct RVO probably.
Shouldn't be a problem whatsoever, should it?

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #6
John Harrison wrote:
[...]
One issue that occurs to me is that the compiler is allowed to optimise away
a copy constructor even if that copy constructor has a side effect. Not sure
if that is an issue here since I can't recall the circumstances in which
this is allowed to happen.


Mostly it's for return value optimization and pass-by-value optimization,
I believe.

V
Jul 22 '05 #7
On Thu, 20 May 2004 16:53:13 +0100 in comp.lang.c++, "John Harrison"
<jo************ *@hotmail.com> wrote,
One issue that occurs to me is that the compiler is allowed to optimise away
a copy constructor even if that copy constructor has a side effect. Not sure
if that is an issue here since I can't recall the circumstances in which
this is allowed to happen.


The compiler is allowed to optimize away the creation of a temporary
value, even if that implies eliminating a constructor call with a side
effect. But, if the compiler finds it necessary to create the object,
then the constructor must be called. Works fine for the purpose of
registering all the objects created.

Jul 22 '05 #8

Speaking RVO , in this situation, there would be the elimination of a
destructor call too,
so the net effect is none.

"bartek" <sp************ ******@o2.pl> wrote in message
news:Xn******** *************** ***********@153 .19.251.200...
"John Harrison" <jo************ *@hotmail.com> wrote in
news:2h******** ****@uni-berlin.de:

(...)
One issue that occurs to me is that the compiler is allowed to
optimise away a copy constructor even if that copy constructor has a
side effect. Not sure if that is an issue here since I can't recall
the circumstances in which this is allowed to happen.


In case of direct RVO probably.
Shouldn't be a problem whatsoever, should it?

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #9

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

Similar topics

10
1872
by: cppaddict | last post by:
Hi, Say I have a abstract base class and I know that every derived class will have a static data member (call it SharedInformation) representing information to be shared across all instances of that derived class. However, this information will be differenet for each derived class. Now, if I define SharedInformation as a static member...
1
1564
by: RainerFaulstich | last post by:
Hi, Some dummy question : Are static member functions of a class indeed a single instance for all instances of the class or have difference instances of this class have its own instances of this function? Rainer
6
1587
by: laniik | last post by:
hi i have a question. i have a class class c { static int vector; c(); void foo(); }; I want to initialize the values in that vector so that in every instance of c, foo can use it. how do i do this? thanks!
8
2030
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the constness of static members. Is there a way to do that? Also, is there a way for a static member function to guarantee constness of static members? TIA...
11
3814
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
8
2880
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
6
7660
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which...
1
1723
by: rhd | last post by:
Hi, - C++ class with a private member function. - Private member function declares a static int locally with an initial value of 0 . - A single instance of the class is created. - The object calls the member function a number of times, each time the local variable is incremented. - Eventually the object calling the member function is...
5
1726
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the members Y is the location of a file to be read. The original design assumes that this class will be instantiated and each instance will happily mange...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8321
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5699
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5370
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3816
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1154
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.