473,226 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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 4114
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: srinivas reddy | last post by:
Hi, Can a constant member function be overloaded with its non-constant equivalent. I mean can void func() be overloaded with void func() const. If so what behaviour is guaranteed, how does the...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
2
by: tekenenSPAM | last post by:
I know that one cannot cast a non-static member function to a void *. (http://users.utu.fi/sisasa/oasis/cppfaq/pointers-to-members.html) However, I have a case where I need to call a certain...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
3
by: xuatla | last post by:
Hi, I have a problem about using a class member function as a parameter in another function. What I tried to implement is something like described below: class A { public:
4
by: free2cric | last post by:
Hi, Just posting a sample progam. class asd's member function calls function b which is not a member function and passes it a class's variable as parameter . Should the called function be a...
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.