Question about static - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Question about static

WerewolfWareWerewolfWare Posts: 304Member
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?

Comments

  • IDKIDK Posts: 1,784Member
    : 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 [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • DonotaloDonotalo Posts: 715Member
    : : 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:
    : [red]A static class is a class that can't create an object.[/red]
    : 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 [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    [purple]
    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.
    [/purple]
  • DonotaloDonotalo Posts: 715Member
    [code]
    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;
    }
    [/code]
    The output will be:
    [code]
    10
    25
    40
    [/code]
    so, the static variable inside static_variable_used() initialized [b]once[/b], and keeps its value inside the function rather than deallocate.
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-9-30 11:20:21[/red][/b][hr]
    : 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
    [code]
    // file a.c
    static int foo()
    {
    return 0;

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

    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.


Sign In or Register to comment.