 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
shaun roe Guest
|
Posted: Wed Jul 19, 2006 9:11 am Post subject: 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::ValidityKey convertToKey(const longlongKey);
};
and then put the implementation in the .cpp file as usual:
in the .cpp file
================
#include "pkg1/ValidityKey.h"
#include "pkg1/ValidityKeyException.h"
pkg1::ValidityKey CoolInterface::convertToKey(const longlongKey){
if (longlongKey > ValidityKey::max()) //max() is 2^63 -1
{
std::cerr<<"CoolInterfaceError: key is too big"<<std::endl;
throw(pkg1::ValidityKeyException("convertToKey",longlongKey));
} else {
return ValidityKey(longlongKey);//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 |
|
| 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
|
|