Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > member function vs standalone func in cpp

Reply
Thread Tools

member function vs standalone func in cpp

 
 
shaun roe
Guest
Posts: n/a
 
      04-09-2008
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
 
Reply With Quote
 
 
 
 
Lloyd Bonafide
Guest
Posts: n/a
 
      04-09-2008
shaun roe <(E-Mail Removed)> wrote in news:shaun.roe-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> 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;
}
 
Reply With Quote
 
 
 
 
Brian Tyler
Guest
Posts: n/a
 
      04-09-2008
I suppose that depends how specific it is to your class. If there is no
way any it would be of use anywhere else then why not make it a private
static? If it may have a more general use then why not have a separate
"utility" namespace where you put all those handy little multipurpose
functions?

static just means that you don't need to create an object of the class to
use the function, as such you can't use "this" in a static, but you can
access private functions and data members. So the class acts a bit like a
namespace and the static is like a friend. However access rules apply, so
if you make it private only the class and friends can access it.

Perhaps there may be some performance advantage over a normal member
function, but I'm not sure about this and it probably depends on the
situation.
 
Reply With Quote
 
Krice
Guest
Posts: n/a
 
      04-09-2008
On 9 huhti, 15:47, shaun roe <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Eberhard Schefold
Guest
Posts: n/a
 
      04-09-2008
shaun roe wrote:

> a) Does this seem reasonable, or is it 'poor style'?


I've asked this question myself (it does appear handy at times), but
decided against it. This is rather a 'gut' feeling, but I think that
the file location of a declaration or definition should be of as little
significance as possible. This may be an academic example, but one day
you could decide to move code into a different file, and thereby
inadvertedly call a different function with the same name that happens
to reside there.

If the utility function doesn't have much to do with the class though, I
don't think you absolutely have to make the function a member. As a C++
programmer one tends to shy away from global functions, but I don't
think that's alway justified. Use a namespace if you want.

> b) is my understanding of static correct? What if I do not declare the
> function static, doesn't it have file scope anyway?


The point is linkage. By default, functions at file scope have external
linkage, and internal linkage if declared static. You'll notice the
difference e.g. when you try to link two object files that both contain
a function with the same name.
 
Reply With Quote
 
shaun roe
Guest
Posts: n/a
 
      04-09-2008
In article <d34Lj.1391$(E-Mail Removed)>,
Brian Tyler <(E-Mail Removed)> wrote:

> I suppose that depends how specific it is to your class. If there is no
> way any it would be of use anywhere else then why not make it a private
> static? If it may have a more general use then why not have a separate
> "utility" namespace where you put all those handy little multipurpose
> functions?


Usually I put it in cpp and if I find I use it in more than 1-2 classes
(depends on the details), I make a utility namespace as you say
>
> static just means that you don't need to create an object of the class to
> use the function, as such you can't use "this" in a static, but you can
> access private functions and data members. So the class acts a bit like a
> namespace and the static is like a friend. However access rules apply, so
> if you make it private only the class and friends can access it.

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.

>
> Perhaps there may be some performance advantage over a normal member
> function, but I'm not sure about this and it probably depends on the
> situation.

 
Reply With Quote
 
Brian Tyler
Guest
Posts: n/a
 
      04-09-2008
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

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      04-09-2008
shaun roe wrote:
> 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.


OK.

> My understanding is that the 'static'
> keyword in this context restricts their scope to the .cpp file.


Well, formally speaking, "scope" in C++ cannot be bigger than one
translation unit (.cpp file) anyway. Declaring a standalone function
'static' doesn't really affect its scope. It restricts its linkage to so
called internal linkage.

But in some informal meaning of the term that's pretty much correct.

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


OK.

> 2 questions:
> a) Does this seem reasonable, or is it 'poor style'?


It is reasonable in case of a more-or-less generic function (like
'checkRange' operating on integral values).

In cases when the function has some semantic attachment to some
particular class (like 'checkRange' on arguments of some user-defined
class type), it might be a good idea to declare it as a static member
function of that class, not as a standalone function.

> b) is my understanding of static correct? What if I do not declare the
> function static, doesn't it have file scope anyway?


This is when the formal meaning of "scope" becomes important. Once
again, 'static' in your case doesn't affect scope. It affects linkage.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-10-2008
On Apr 9, 2:47 pm, shaun roe <(E-Mail Removed)> wrote:
> 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'?


I do it all the time, except that I'll put the function in an
anonymous namespace, rather than declaring it static. And I
don't limit it to functions: data and helper classes will appear
as well.

In extreme cases, I'll create a private namespace in the header,
forward declare a class there, and make it a friend. That way,
my "helper" functions (which are then members of this friend
class) can access private members as well.

> b) is my understanding of static correct? What if I do not
> declare the function static, doesn't it have file scope
> anyway?


Formallly:
-- there is no such thing as file scope; it's namespace scope,
and
-- scope is always limited to a file, what you're concerned
with is called linkage (whether the same qualified name in
another translation unit refers to the same "thing" or not).

Practically, declaring a function or a variable at namespace
scope static means that it has "internal linkage"; the same name
elsewhere in the translation unit refers to the same thing, but
the same name in other translation units doesn't.
(Pragmatically: the name won't be exported to the linker.)

Using static for this has been deprecated (I think), and of
course, it can't be used for helper classes, etc. (And you can't
bind a template argument to anything which doesn't have external
linkage. Not often a consideration for functions, but it can be
for variables.) Putting the functions etc. in an anonymous
namespace means that they have external linkage, but the
qualified name (i.e. the name with its namespace) cannot be
specified in another translation unit.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
func() in implementation and func (100, 200, 300) in calling Alex Vinokur C Programming 6 11-18-2006 08:08 AM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
difference between import from mod.func() and x=mod.func() Hari Sekhon Python 0 06-20-2006 08:07 AM
What's the difference between built-in func getattr() and normal call of a func of a class Johnny Python 3 08-23-2005 08:28 AM
int func() v/s int func(void) Vinko Vrsalovic C Programming 14 01-24-2005 08:07 AM



Advertisments