473,568 Members | 2,898 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 6521
"Koen" <no@ssppaamm.co m> 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 be invoked is from inside m1. Is either style preferred? Here are two short examples: Helper function as method:
2
1494
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 checkforimg(ByVal Imagesubprod1 As String) If Imagesubprod1 <> "" then response.write(Imagesubprod1) Else response.write("nbsp;") End If
8
5722
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 simplest form, a BHO is a COM in-
1
1329
by: Tran Hong Quang | last post by:
Hello, What is helper function concept? I am new to C. Thanks Tran Hong Quang
1
3223
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 helper functions to convert between the types used, in particular between 'unsigned long long' and a ValidityKey which is like an unsigned long long...
6
5822
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 forth between python dictionaries and SQL tables. def MySQLSelect(table, arglist=, argdict={}, **kwargs): """Build an SQL SELECT command from the...
6
3521
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 Hidden_functionality { protected: // These helper classes provide some functionality that is // only used by the client classes class Helper1 {};
6
49713
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
4798
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): """Build an SQL SELECT command from the arguments: Return a single string which can be 'execute'd. arglist is a list of strings that are...
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7660
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6275
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
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 we have to send another system
0
932
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.