C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Question about static Posted by WerewolfWare on 30 Sept 2005 at 5:17 AM
Hi, I've recently noticed a wide use of static varibles, functions, class etc'. When I've learnt about static uses, the explainations were extremely vague and no examples were given. Can someone tell me what the definition of static is?
Report
Re: Question about static Posted by IDK on 30 Sept 2005 at 5:52 AM
: Hi, I've recently noticed a wide use of static varibles, functions, class etc'. When I've learnt about static uses, the explainations were extremely vague and no examples were given. Can someone tell me what the definition of static is?
:
I would define it as follows:
A static class is a class that can't create an object.
A static variable is a variable that contains it's value between the calls.

I think it works like this in C++

The one and only Niklas Ulvinge aka IDK

Report
Re: Question about static Posted by Donotalo on 30 Sept 2005 at 7:43 AM
: : Hi, I've recently noticed a wide use of static varibles, functions, class etc'. When I've learnt about static uses, the explainations were extremely vague and no examples were given. Can someone tell me what the definition of static is?
: :
: I would define it as follows:
: A static class is a class that can't create an object.
: A static variable is a variable that contains it's value between the calls.
:
: I think it works like this in C++
:
: The one and only Niklas Ulvinge aka IDK

there is nothing called static class in c++. an object of a class is same as a static variable. it keeps itself between the execution of that block of code where it resides.

two other things:

(1) a static member variable of a class is a special kind of variable, which is shared among all the instances of that class. it can also be accessed without declaring any object of that class.

(2) a static member function of a class is a special kind of function, which can be called without declaring any object of that class. a static member function can only access other static variables and can only call other static member function of that class.

Report
What do you mean "between calls"? Posted by WerewolfWare on 30 Sept 2005 at 9:46 AM

Report
Re: What do you mean "between calls"? Posted by Donotalo on 30 Sept 2005 at 10:27 AM
void static_variable_used() {
    static a = 10;
    cout << a << endl;
    a += 15;
}

int main() {
    static_variable_used();
    static_variable_used();
    static_variable_used();
    return 0;
}

The output will be:
10
25
40

so, the static variable inside static_variable_used() initialized once, and keeps its value inside the function rather than deallocate.
Report
Re: Question about static Posted by stober on 30 Sept 2005 at 11:19 AM
This message was edited by stober at 2005-9-30 11:20:21

: Hi, I've recently noticed a wide use of static varibles, functions, class etc'. When I've learnt about static uses, the explainations were extremely vague and no examples were given. Can someone tell me what the definition of static is?
:

One other point others have not mentioned. A static function can only be called from other functions within the same *.c or *.cpp file in which it resides. That means static function foo() cannot be called from a function in another source file. It also means two source files can declare static function(s) with the same name
// file a.c
static int foo()
{
  return 0;

}

// file b.c
static char* foo(int x)
{
   char* p = malloc(x);
   return p;
}


When the above two files are compiled and linked into the same program, the linker will not generate duplicate function declaractions because the functions are static and visible only within the file in which they reside.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.