static function, not a member?

H

Howard

Hi,

I came across some sample code for using a third-party API, and the code
in it had several free-standing (i.e., non-member) functions, but strangely,
these were declared as 'static'. My compiler tells me that the function has
no prototype, which is weird, because there *is* a prototype in the header
file. I was not able to find in my books any reference to a static function
that was not a member function. The closest info I could find (via google)
was something about making the symbol "local", and a suggestion that this
was deprecated in C++.

Can someone tell me what it means to have the static keywoard in front
of a non-member function, and whether it can be safely removed for my use?

Thanks,
Howard
 
J

John Carson

Howard said:
Hi,

I came across some sample code for using a third-party API, and
the code in it had several free-standing (i.e., non-member)
functions, but strangely, these were declared as 'static'. My
compiler tells me that the function has no prototype, which is weird,
because there *is* a prototype in the header file. I was not able to
find in my books any reference to a static function that was not a
member function. The closest info I could find (via google) was
something about making the symbol "local", and a suggestion that this
was deprecated in C++.

Can someone tell me what it means to have the static keywoard in
front of a non-member function, and whether it can be safely removed
for my use?


It means that the function is only available within the translation unit
within which it is defined.
 
J

JKop

Howard posted:
Hi,

I came across some sample code for using a third-party API, and the
code
in it had several free-standing (i.e., non-member) functions, but
strangely, these were declared as 'static'. My compiler tells me that
the function has no prototype, which is weird, because there *is* a
prototype in the header file. I was not able to find in my books any
reference to a static function that was not a member function. The
closest info I could find (via google) was something about making the
symbol "local", and a suggestion that this was deprecated in C++.

Can someone tell me what it means to have the static keywoard in
front
of a non-member function, and whether it can be safely removed for my
use?

Thanks,
Howard

EXAMPLE 1

void Horse(void)
{
static int j = 7;

j +=2 ;
}

int main(void)
{
Horse();
Horse();
Horse();
Horse();
Horse();

// The variable j is global, but can only be accessed from
// within the Horse function. Right now, it's value is 17.
}


EXAMPLE 2

static int hello = 45;

int main(void)
{
;
}

The global variable hello cannot be accessed from outside the current CPP
file.
 
J

Jack Klein

Howard posted:


EXAMPLE 1

void Horse(void)
{
static int j = 7;

j +=2 ;
}

int main(void)
{
Horse();
Horse();
Horse();
Horse();
Horse();

// The variable j is global, but can only be accessed from
// within the Horse function. Right now, it's value is 17.
}


EXAMPLE 2

static int hello = 45;

int main(void)
{
;
}

The global variable hello cannot be accessed from outside the current CPP
file.

You misread the question. It dealt with static _functions_, not
static objects.
 
M

Minti

Howard said:
Hi,

I came across some sample code for using a third-party API, and the code
in it had several free-standing (i.e., non-member) functions, but strangely,
these were declared as 'static'. My compiler tells me that the function has
no prototype, which is weird, because there *is* a prototype in the header
file. I was not able to find in my books any reference to a static function
that was not a member function. The closest info I could find (via google)
was something about making the symbol "local", and a suggestion that this
was deprecated in C++.

Can someone tell me what it means to have the static keywoard in front
of a non-member function, and whether it can be safely removed for my use?

Thanks,
Howard


When you add a static keyword in front of a function definition, it
means that the definition of the function would be limited to that
very particular translation unit or that very particular file.


e.g. if your header file includes a lot of functions that you want
your client to use, it is but natural that you do not want to limit
your coding to those very particular functions so you bring in static
functions whose 'usablility' would be reserved to that very particular
translation-unit / file itself only.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top