Need Help

Hi guys i am very interested what is Static Variables ? :/ can you give me a definition and example of it in easy use of English? I know what is Global variables and Local variables but i do not have any knowledge of static Variables and im quite broken.

And for the last question what is how to use and format comments? What does it mean by formatting comments?
keyword static has several different meanings. Most common one is static variables in function or class scope.

In function scope static variable persist and keeps its value between executions:
1
2
3
4
5
6
7
8
9
10
void foo(int x)
{
    static int i = 0;
    i += 3;
    std::cout << i << '\n';
}
//...
foo(3);
foo(2);
foo(1);
3
5
6


In class scope static variable is shared between all instances of the class and even can be accessed without instance at all
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct foo()
{
    int i = 0;
    static int j = 0;
}
//...

foo bar;
foo baz;
bar.i = 2;
bar.j = 3;
foo::i = 1;
std::cout << bar.i << ' ' << bar.j << '\n';
std::cout << baz.i << ' ' << baz.j << '\n';
2 1
0 1


In global scope keyword static means that variable has internal linkage (not visible from other compilation units)



What does it mean by formatting comments?
To make them look good and easy to read:
1
2
3
4
5
6
7
std::cout << '1' << //bad formatting
          << "234" << //if all three comment lines
          << "56"; //are related

std::cout << '1' <<   //better formatting
          << "234" << //if all three comment lines
          << "56";    //are related 

Last edited on
Can you elaborate by formatting comments ? You mean the more comments then better?
You mean the more comments then better?
Amount and quality of comments are orthogonal to their formatting.

Formatting comments is the same thing as formatting source code: making them easier to understand and reason about.
Topic archived. No new replies allowed.