Two questions - thanks!

C

Chutian

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!
 
D

David White

Chutian said:
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
 
D

David Lindauer

Chutian said:
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
 
S

Scuro

Chutian said:
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."
 
G

gipsy boy

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
 
C

Chutian

Scuro said:
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.
 
S

Scuro

Chutian said:
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.

Something like that:

class One{};

class Two{};

if you want to share data between two classes, you can do the following:

class Zero{
static int i;
};

class One:public Zero{
}

class Two:public Zero{
}

Now is possible to communicate between different Ones, different Twos,
and Ones and Twos. You can also add code to estabilish some sort of
communication politic.

To do that, the inheritance must make sense. For example, you can
inherit Square and Circle from Shape, but is not elegant to inherit
Circles and Cars from a superclass called "Things" :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top