 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Koen Guest
|
Posted: Thu Jan 27, 2005 2:48 pm Post subject: Helper functions |
|
|
Hi all,
I have a question about something rather common, but I'm a bit lost at
the moment:
Lat's say you have this:
// in A.h
Class A
{
public:
A();
~A();
void DoIt(bool inSpecial);
}
where the DoIt function should do something different depending on the
given bool:
// in A.cpp
void A::DoIt(bool inSpecial)
{
if (inSpecial)
DoItNormal();
else
DoItSpecial();
}
and the DoIt... functions do not need access to the class.
You can now implement the two DoIt... functions in several ways:
1. make them private member functions of the class:
This is not strictly necessary, since they do not access class data.
But users of the class will see the two helper functions in the A.h
header (which is something I'd like to avoid).
2. put them in the A.cpp file:
This way, these functions will become global functions, which is
something I'd like to avoid.
3. put them in the A.cpp file with the "static" keyword:
This way, they will only be visible at file-scope.
I'd think 3. would be the nicest solution, but what about thread safety?
If these two functions do rather lengthy calculations and they are
called from multiple threads, couldn't that lead to problems (storage
used in the static function accessed from two threads)? Is this less of
a problem if you're using approach 1. (private member functions)? I
think not, right?
Koen
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Thu Jan 27, 2005 3:23 pm Post subject: Re: Helper functions |
|
|
"Koen" <no (AT) ssppaamm (DOT) com> wrote
Hi,
| Quote: | I have a question about something rather common, but I'm a bit lost at
the moment:
Lat's say you have this:
// in A.h
Class A
{
public:
A();
~A();
void DoIt(bool inSpecial);
}
where the DoIt function should do something different depending on the
given bool:
// in A.cpp
void A::DoIt(bool inSpecial)
{
if (inSpecial)
DoItNormal();
else
DoItSpecial();
}
and the DoIt... functions do not need access to the class.
NB: then DoIt(bool) itself could be declared as a static member |
of class A.
| Quote: | You can now implement the two DoIt... functions in several ways:
1. make them private member functions of the class:
This is not strictly necessary, since they do not access class data.
But users of the class will see the two helper functions in the A.h
header (which is something I'd like to avoid).
Agreed. |
| Quote: | 2. put them in the A.cpp file:
This way, these functions will become global functions, which is
something I'd like to avoid.
Note that you can put the two functions in an anonymous namespace, |
which will 'hide' them from other translation units:
namespace {
void f() // will be hidden from the outside
{ ..... }
}
| Quote: | 3. put them in the A.cpp file with the "static" keyword:
This way, they will only be visible at file-scope.
I'd think 3. would be the nicest solution, but what about thread safety?
In terms of thread safety, there is absolutely no difference between |
static or non-static global functions. (This is not to be confused with
static variables declared in function scope, which may have issues).
| Quote: | If these two functions do rather lengthy calculations and they are
called from multiple threads, couldn't that lead to problems (storage
used in the static function accessed from two threads)? Is this less of
a problem if you're using approach 1. (private member functions)? I
think not, right?
It is absolutely the same. Function-scope variables in a static function |
are not using static variable storage -- unless specified explicitly.
Local variables are still stack-based (using automatic storage) by default.
So the correct approach is either #2(with anonymous namespace), or #3.
Note that static functions have some limitations when using templates,
so the C++ standard has deprecated static functions (#3). But if the
template issue does not affect you, #3 is a perfectly ok and valid choice.
Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jan 27, 2005 3:27 pm Post subject: Re: Helper functions |
|
|
Koen wrote:
| Quote: | I have a question about something rather common, but I'm a bit lost at
the moment:
Lat's say you have this:
// in A.h
Class A
{
public:
A();
~A();
void DoIt(bool inSpecial);
}
where the DoIt function should do something different depending on the
given bool:
// in A.cpp
void A::DoIt(bool inSpecial)
{
if (inSpecial)
DoItNormal();
else
DoItSpecial();
}
and the DoIt... functions do not need access to the class.
You can now implement the two DoIt... functions in several ways:
1. make them private member functions of the class:
This is not strictly necessary, since they do not access class data.
But users of the class will see the two helper functions in the A.h
header (which is something I'd like to avoid).
2. put them in the A.cpp file:
This way, these functions will become global functions, which is
something I'd like to avoid.
3. put them in the A.cpp file with the "static" keyword:
This way, they will only be visible at file-scope.
|
4. Same as 3, but don't declare them 'static' and instead put them in
the anonymous namespace. This is the preferred method.
| Quote: | I'd think 3. would be the nicest solution, but what about thread safety?
|
C++ has no concept of a "thread". That usually means that nothing is
"thread-safe" unless you make it such.
| Quote: | If these two functions do rather lengthy calculations and they are
called from multiple threads, couldn't that lead to problems (storage
used in the static function accessed from two threads)?
|
It definitely could especially if they use shared data. If they don't
use shared data, what problems do you see?
| Quote: | Is this less of
a problem if you're using approach 1. (private member functions)? I
think not, right?
|
AFAIK, "thread safety" is completely orthogonal to membership or linkage.
V
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|