K
Koen
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:oIt(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
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:oIt(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