473,486 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Two questions - thanks!

What's the best way to share data between classes?

What's the purpose of define a member function in a class as 'static'? Just
for it to access static member variables? Is there any other reasons?

Thanks!
Jul 22 '05 #1
5 1250
"Chutian" <Ch*****@168.com> wrote in message
news:GF*******************@bgtnsc05-news.ops.worldnet.att.net...
What's the best way to share data between classes?
This question is too general. In what form is the data? Do the classes that
share the data always want to share it? Is the data a compile-time constant?
And there are probably many more questions that one could ask.
What's the purpose of define a member function in a class as 'static'?
In essence, a static function is a class-wide function, meaning that it is
relevant to the class but not to any specific instance of the class.
Just
for it to access static member variables?
That's one possible use for it, though class instances can also access
static variables, so you don't have to write a static function to access
static member variables.
Is there any other reasons?


Many of them. There's a class of mine that has 15 static member functions
for various purposes. The class is the base class of a hierarchy of wrapper
classes for objects stored in a database. Most of the static functions
perform certain operations on the database directly, without having to
actually create a wrapper object. A couple of them lock and unlock a mutex
used to protect the database's integrity. The static functions are called by
non-static functions and can also be called publicly.

Here's a very simple use for a static function:

class Rectangle : public Shape
{
public:
// ..
static double Area(double length, double width);
double Area() const { return Area(mLength, mWidth); }

private:
double mLength;
double mWidth;
// other stuff
};

This Rectangle class might be primarily used in a graphics library, for
example, and might carry a lot of baggage not shown here (drawing resources
etc.). Obviously, a Rectangle object must know how to compute its area, but
should you have to create a Rectangle object just to do such a simple
calculation? Static functions allow you to do some operations that a class
should know how to do, but without having to do the unnecessary work of
creating an instance of the class beforehand. Also, non-static members can
use the same static functions to ensure consistency.

DW

Jul 22 '05 #2


Chutian wrote:
What's the best way to share data between classes?

What's the purpose of define a member function in a class as 'static'? Just
for it to access static member variables? Is there any other reasons?

Thanks!


you can access static member variables from within a non-static function if you
want. The main reason for using 'static' is if you have to call the function
without having a specific object to operate on, one example is if the class
contains a list of pointers to objects of the class and you want to iterate
through the list you wouldn't necessarily want to use a non-static function to
do it.

David
Jul 22 '05 #3
Chutian wrote:
What's the best way to share data between classes?
To share data between objects of the same class i suggest to use a
static data, to share data between two different classes, there are many
solutions. One quite elegant solution, if it makes sense, is to inherit
from a common superclass that shares the data.
What's the purpose of define a member function in a class as 'static'? Just
for it to access static member variables? Is there any other reasons?


From "thinking in C++":
"static member functions

You can also create static member functions that, like static data
members, work for the class as a whole rather than for a particular
object of a class. Instead of making a global function that lives in
and “pollutes” the global or local namespace, you bring the
function inside the class. When you create a static member
function, you are expressing an association with a particular class."
Jul 22 '05 #4
What's the purpose of define a member function in a class as 'static'? Just
for it to access static member variables? Is there any other reasons?


There can be another reason. If you want to have a class' non-static
member functions act as C callback functions (like what you need in
pthread.h), sometimes you'll get a problem because every non-static
member function gets an anonymous "this" parameter with it. The library
then doesn't recognize the type of that function as a normal C function,
because its parameters are wrong.
Static funcitons don't get a "this" pointer and can provide an easy
workaround in these situations.
This could be confusing however if you have no idea what I'm talking
about..just remember it for the future then

--
- gipsy boy
Jul 22 '05 #5
Scuro <cr****@questolevalo.libero.it> wrote in message
news:co**********@lacerta.tiscalinet.it...
Chutian wrote:
What's the best way to share data between classes?


To share data between objects of the same class i suggest to use a
static data, to share data between two different classes, there are many
solutions. One quite elegant solution, if it makes sense, is to inherit
from a common superclass that shares the data.


Thanks! What do you mean by "... to inherit from a common superclass that
shares the data."

If the shared data is a non-staic member variable, it will be separate
copies of it among the separate objects of the derived classes. Of course,
if it is static, then it will work.
Jul 22 '05 #6

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

Similar topics

4
2214
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
5
3668
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
3
1158
by: bredal Jensen | last post by:
I'm currently downloading the Microsoft "Visual web developer 2005 Express" and i have a few questions in case someone could have insider informations about this. I have just attended a seminar...
6
1222
by: dw | last post by:
Hello, all. We're building a Web app where we can have any number of questions, which can contain any type of form element -- check boxes, text fields, radio buttons, dropdowns, etc. We'd like to...
30
15389
by: James Conrad StJohn Foreman | last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers to go work for a SQL Server shop instead. In order to find my replacement, they're trying to put together a set of questions...
16
1700
by: marc_r_bertrand | last post by:
To all asp/db pros: The quiz code below works. But there is a problem when too many questions are answered (radio buttons clicked). I am not an asp pro. So, is there a pro out there or an...
1
1458
by: robertmeyer1 | last post by:
Hi, I have 3 tables set up. tblQuestion, tblAnswer, tblClient. I have them linked together and have a sbf and mainform set up for data entry. The sbf links the questions and answers together. ...
4
2493
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
5
3859
by: chrisplanters | last post by:
Hi .. are you a dot net developer. If yes... then please visit http://dotnetinterviews.blogspot.com This website is dedicated to most commonly asked questions in dot net interviews. please...
16
2071
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using Visual C# window to dispaly a set of questions with their answers. The users should be able to move to the next question by clicking on next button. I am going to use only one panel...
0
7105
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,...
0
7180
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...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
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...
0
5439
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,...
1
4870
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...
0
3076
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...
0
1381
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 ...
0
266
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...

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.