Archived

This topic is now archived and is closed to further replies.

Alpha_ProgDes

static function.... a quickie

Recommended Posts

since i''ve never seen an explanation of this in any C++ book i''ve seen. i thought i''d ask here.
  
static float area_of_circle (int radius)
{
    float area;
    float PI = 3.141519;

    return area = PI * radius * radius;
}


static void any_type_of_function (int& aNumber, int& aNumber2)
{
    int anyNumber = 10;
    int thisDigit = 20;
    int movingInt = 50;    

    aNumber += anyNumber;
    aNumber2 *= aNumber;
    thisDigit += (aNumber2 * movingInt);

    ++movingInt;
}
  
now my question for these convoluted functions is: since the functions are declared static they don''t lose scope? and if they don''t then that means the values in the variables do not get "destroyed". am i correct?

Share this post


Link to post
Share on other sites
static member functions are not the same as static variables. Static member functions are simply member functions that are called on the class, not the object. Because of that, they can't access any non-static members (since the members would have to be bound to a particular object, not just the class).

Static variables are the things you were talking about that keep their value between successive calls.

EDIT: oh, and static non-member functions are a carryover from C++ that is now deprecated, replaced by anonymous namespaces. I swear, the designers of C++ reused keywords like rubber sheets. Just look at const.


How appropriate. You fight like a cow.

[edited by - sneftel on April 28, 2003 5:05:11 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
static member functions are not the same as static variables. Static member functions are simply member functions that are called on the class, not the object. Because of that, they can''t access any non-static members (since the members would have to be bound to a particular object, not just the class).

Static variables are the things you were talking about that keep their value between successive calls.

EDIT: oh, and static non-member functions are a carryover from C++ that is now deprecated, replaced by anonymous namespaces. I swear, the designers of C++ reused keywords like rubber sheets. Just look at const.


How appropriate. You fight like a cow.

[edited by - sneftel on April 28, 2003 5:05:11 AM]


i meant static non-member functions.
they are a carryover from C++? i take it you mean pre-standard C++ or even (ANSI?)C.

anonymous namespaces? well if you don''t mind a brief description/definition of that, it would be much appreciated.

also if i would to put those two functions in some arbitrary code how would they be processed? i take it that static non-member functions do not act like static variables.

Share this post


Link to post
Share on other sites
Static non-member functions are not visible outside of the compilation unit they are defined in. It is the C equivalent of private.



"Laughter means distance. Where laughter is absent, madness begins. The moment one takes the world with complete seriousness one is potentially insane. The whole art of learning to live means holding fast to laughter; without laughter the world is a torture chamber, a dark place where dark things will happen to us, a horror show filled with bloody deeds of violence."
-- Jens Bjørneboe

Share this post


Link to post
Share on other sites
Er, a carryover from C. *smacks self with the go-to-sleep-already bat*

And anonymous namespaces? well, it goes like this:


  
void AFunctionYouCanCallFromAnywhere()
{
// ...

SecretHelperFunction();
// ...

}

namespace
{
void SecretHelperFunction() // can only be called by

{ // functions declared within

// ... // this file

}
}


How appropriate. You fight like a cow.

[edited by - sneftel on April 28, 2003 5:40:22 AM]

Share this post


Link to post
Share on other sites
i''ll be damned... there is a way to do OOD in C
now! since unfortunately i never bothered to learn the technical terms such as "compilation unit" i''m going to take a little tour through the state of Google.

if anyone can answer my latest post "completely", it''ll be much obliged.

thanks, Arlid. thanks, Sneftel.

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
Static non-member functions are not visible outside of the compilation unit they are defined in. It is the C equivalent of private.



"Laughter means distance. Where laughter is absent, madness begins. The moment one takes the world with complete seriousness one is potentially insane. The whole art of learning to live means holding fast to laughter; without laughter the world is a torture chamber, a dark place where dark things will happen to us, a horror show filled with bloody deeds of violence."
-- Jens Bjørneboe



does compilation unit mean (header) file?

Share this post


Link to post
Share on other sites
quote:
Original post by Alpha_ProgDes
does compilation unit mean (header) file?

A translation unit is what you get after the preprocessor has been run on a file and expanded out all the inclusions.

File-scope static is a means of specifying internal linkage. When something has internal linkage, it cannot be "seen" by anything external to the translation unit.

The code you posted has nothing to do with the question of scope, since you haven't declared any variables as static, only functions - you've given them internal linkage. If you meant to ask about the scope of static variables, then what happens is this:

i) They get constructed during the static initialisation phase, before main() is entered.

ii) They get destructed when returning from main() or as a result of calling exit(). They are destroyed in the reverse order that the constructors completed.

There are a couple of special-cases which you probably don't need to know about.

[edited by - SabreMan on April 28, 2003 7:17:06 AM]

Share this post


Link to post
Share on other sites
No, a .c(pp) file, with all its includes inserted - basically what eventually ends up as a .o(bj). Static functions cannot be called from other compilation units than the one it is declared in, and you''ll get a linker error if you attempt to do so.



"To assert that the earth revolves around the sun is as erroneous as to claim that Jesus was not born of a virgin."
-- Cardinal Bellarmine

Share this post


Link to post
Share on other sites