473,508 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 23 '05 #1
2 6514
"Koen" <no@ssppaamm.com> wrote in message
news:ct**********@gaudi2.UGent.be...
Hi,
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.
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.
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
{ ..... }

}
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).
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

Jul 23 '05 #2
Koen wrote:
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.
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.
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?
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
Jul 23 '05 #3

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

Similar topics

4
8884
by: Robert Ferrell | last post by:
I have a style question. I have a class with a method, m1, which needs a helper function, hf. I can put hf inside m1, or I can make it another method of the class. The only place hf should ever...
2
1491
by: Chumley the Walrus | last post by:
I'm using a subroutine/helper function display an image (the image would be displayed inside a datalist control's <itemtemplate> ) <script language="VB" runat="server"> Public Sub...
8
5718
by: Joe Johnston | last post by:
I need a Browser Helper object written in VB.NET Please point me at a good example. Joe MCPx3 ~ Hoping this MSDN ng three day turnaround is true. Additional info: What is a BHO? In its...
1
1318
by: Tran Hong Quang | last post by:
Hello, What is helper function concept? I am new to C. Thanks Tran Hong Quang
1
3215
by: shaun roe | last post by:
When should a function be a private member, and when simply a standalone function in the .cpp file? I'm in the middle of writing a class which bridges between two packages, and so I need some...
6
5816
bartonc
by: bartonc | last post by:
Here is the first installment of SQL helper functions which I use with two classes that I wrote which encapsulate a MySQL server and a MySQL client. These helpers make it easy to convert back and...
6
3519
by: mailforpr | last post by:
Suppose you have a couple of helper classes that are used by 2 client classes only. How can I hide these helper classes from other programmers? Do you think this solution is a good idea?: class...
6
49700
by: Mike P | last post by:
I have heard about helper classes, but can somebody please explain to me what they are and what they are used? *** Sent via Developersdex http://www.developersdex.com ***
0
4792
bartonc
by: bartonc | last post by:
Here are the latest versions of My (as in mine) SQL helper functions. Please feel free to rename them if you use them. The SELECT helper:def MySQLSelect(table, arglist=(), argdict={}, **kwargs): ...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7036
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
7489
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
5624
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
5047
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
4705
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...
0
3191
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...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
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.