473,659 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should helper functions be members?

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 but only goes up to 2^63 -1.
(Please dont flame me for using unsigned long long, I know its not
standard, but I'm stuck with it).

So I could do the following (this example is not complete - and is just
off the top of my head - , just highlighting the salient points)

in the .h file:
==============
//fwd decl
namespace pkg1{
class ValidityKey;
}

class CoolInterface{
private:
pkg1::ValidityK ey convertToKey(co nst longlongKey);
};

and then put the implementation in the .cpp file as usual:

in the .cpp file
=============== =

#include "pkg1/ValidityKey.h"
#include "pkg1/ValidityKeyExce ption.h"

pkg1::ValidityK ey CoolInterface:: convertToKey(co nst longlongKey){
if (longlongKey ValidityKey::ma x()) //max() is 2^63 -1
{
std::cerr<<"Coo lInterfaceError : key is too big"<<std::endl ;
throw(pkg1::Val idityKeyExcepti on("convertToKe y",longlongKey) );
} else {
return ValidityKey(lon glongKey);//this constructor exists
}
}

Now... equally I could just put the whole helper function in the .cpp
file as a standalone function and not put any mention of it in my class,
so I wouldnt need a fwd declaration in my .h file for the class. I think
this would be cleaner, but its not a philosophy I see followed very
often... what is the disadvantage? I will only use this helper in the
scope of the .cpp file.

cheers

shaun
Jul 19 '06 #1
1 3227
In article
<sh************ *************** **@news-reader.wanadoop ortails.com>,
shaun roe <sh*******@wana doo.frwrote:
When should a function be a private member, and when simply a standalone
function in the .cpp file?
Make it a stand-alone function in the cpp file (in an unnamed namespace)
if the number of parameters it needs is relatively small. This will help
reduce your compile times.
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 but only goes up to 2^63 -1.
(Please dont flame me for using unsigned long long, I know its not
standard, but I'm stuck with it).

So I could do the following (this example is not complete - and is just
off the top of my head - , just highlighting the salient points)

in the .h file:
==============
//fwd decl
namespace pkg1{
class ValidityKey;
}

class CoolInterface{
private:
pkg1::ValidityK ey convertToKey(co nst longlongKey);
};

and then put the implementation in the .cpp file as usual:

in the .cpp file
=============== =

#include "pkg1/ValidityKey.h"
#include "pkg1/ValidityKeyExce ption.h"

pkg1::ValidityK ey CoolInterface:: convertToKey(co nst longlongKey){
if (longlongKey ValidityKey::ma x()) //max() is 2^63 -1
{
std::cerr<<"Coo lInterfaceError : key is too big"<<std::endl ;
throw(pkg1::Val idityKeyExcepti on("convertToKe y",longlongKey) );
} else {
return ValidityKey(lon glongKey);//this constructor exists
}
}
Hmm. This function should be part of ValidaityKey. If you can't change
ValidaityKey then I would make a separate module for it and make it a
stand-alone function. There is no reason at all to put it in
CoolInterface.
Jul 19 '06 #2

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

Similar topics

19
2332
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
2
1701
by: Mark Buxbaum | last post by:
Hi, Is is possible to use a collection class instance as a helper object in a class? For example: MyClass.h: ---------- class MyClass {
3
4418
by: James | last post by:
Hi - I need to implement the GetIfEntry function from the Ip Helper API and am having some real problems converting the MIB_IFROW structure to C# - In particluar I cannot fiqure out how to declare it properly. I realise that it requires some additional parameters on some of its members attributes (like Charset=Unicode etc). So can anyone please fill me in on the declaration side of the MIB_IFROW structure please - layout and other...
1
1855
by: D. Shane Fowlkes | last post by:
Hello All. I keep asking for help with this on the www.asp.net forums and nobody seems to be able to help. What I'm trying to accomplish is very simple. I simply want to create a Hyperlink Column in a Datagrid and reformat the text output of the column. The helper function seems to be working but the hyperlink column isn't properly being render into html. The actual hyperlink web control is showing up in the html when tested on two...
8
5726
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
1362
by: Tran Hong Quang | last post by:
Hello, What is helper function concept? I am new to C. Thanks Tran Hong Quang
7
2789
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an object of that class. And is that also the reason why if you use class member functions for operators << and >you have to write: someclass << cout; someclass >cin; ?
6
3525
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 {};
37
1821
by: nolo contendere | last post by:
I've recently begun playing with prototype.js as well as scriptaculous, and found both very helpful in developing web pages with lots of pop. Modal boxes, SlideUp/Down, calendars, etc. I've also recently ramped up my lurking and participation in this newsgroup. There are some who advocate using prototype, and others who are adamantly opposed to it. Are there any concrete reasons why I should NOT use it? Is it inefficient? It seems that...
0
8428
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8528
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2752
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
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.