468,484 Members | 1,751 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

member function vs standalone func in cpp

This may be a question of style:
Sometimes I need a little helper function (e.g. checkRange(val, lo, hi))
in my .cpp and have a habit of making these functions simple standalone
functions in the .cpp file instead of member functions, because they
don't access any member variables. My understanding is that the 'static'
keyword in this context restricts their scope to the .cpp file.
If I find I use the function a lot, I promote it to a fully fledged
standalone function in a standalone .h & .cpp file as usual.

2 questions:
a) Does this seem reasonable, or is it 'poor style'?
b) is my understanding of static correct? What if I do not declare the
function static, doesn't it have file scope anyway?

cheers

shaun
Apr 9 '08 #1
3 3762
shaun roe <sh*******@wanadoo.frwrote in news:shaun.roe-
1B*******************@news.wanadoo.fr:
2 questions:
a) Does this seem reasonable, or is it 'poor style'?
Nothing wrong with static helper functions.
b) is my understanding of static correct? What if I do not declare the
function static, doesn't it have file scope anyway?
If you don't declare it static, modules that link with it can call it
provided they declare it (or include the declaration from a header
file):

<bar.cpp>

int get_number()
{
return 5;
}

<foo.cpp>

#include <iostream>

int get_number(); // Declaration

int main()
{
std::cout << get_number() << std::endl;

return 0;
}
Apr 9 '08 #2
On 9 huhti, 15:47, shaun roe <shaun....@wanadoo.frwrote:
Sometimes I need a little helper function (e.g. checkRange(val, lo, hi))
in my .cpp and have a habit of making these functions simple standalone
functions in the .cpp file instead of member functions, because they
don't access any member variables.
val, lo and hi should be member variables.
Apr 9 '08 #3
On Wed, 09 Apr 2008 17:36:50 +0200, shaun roe wrote:
I didn't mean static as in static meber function, but the other meaning
of static, using it in the global namespace to restrict the scope of the
function to a single file; as I understand, 'static' in this usage in a
cpp file:
static void foo(){std::cout<<"Are you local?";}

will restrict the scope of foo to the cpp file only. I believe this has
been confirmed by the other commentators on this thread.
Got you, everything I write is template / inline so anything static ends
up everywhere anyway :)

Apr 9 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by srinivas reddy | last post: by
2 posts views Thread by tekenenSPAM | last post: by
15 posts views Thread by Albert | last post: by
3 posts views Thread by xuatla | last post: by
4 posts views Thread by free2cric | last post: by
5 posts views Thread by Tim Frink | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.