what is the difference between static and normal v

2020-07-23 06:23发布

I need to know the difference,am beginner.

标签: c++
9条回答
一纸荒年 Trace。
2楼-- · 2020-07-23 06:55
void func()
{
    static int static_var=1;
    int non_static_var=1;

    static_var++;
    non_static_var++;

    cout<<"Static="<<static_var;
    cout<<"NonStatic="<<non_static_var;
}

void main()
{
    clrscr();
    int i;
    for (i=0;i<5;i++)
    {
        func();
    }
    getch();
}

The above gives output as:

Static=2
Nonstatic=2

Static=3
Nonstatic=2

Static=4
Nonstatic=2

Static=5
Nonstatic=2

Static=6
Nonstatic=2

Static variable retains its value while non-static or dynamic variable is initialized to '1' every time the function is called. Hope that helps.

查看更多
虎瘦雄心在
3楼-- · 2020-07-23 06:58

suppose class A has static variable x... and not static variable p

now if you create hundred instance of class A (i.e A a;) x would be shared among this hundred instance... but there would be hundred copies of p... one copy of p per instance of A

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-07-23 07:00
  • static namespace scope variables have internal linkage, while non-static namespace scope variables have external linkage by default! Details: a const namespace scope variable has internal linkage by default. That linkage can by changed by keyword extern.
  • static variables in a class is associated with the class, that means, all instances of the class have the same instances of the static variables; they’re like a global variables that every instance of the same class has access to.
  • non-static variables in a class are instance members, that is, every instance of the class will have its own instances of the non-static variables.
  • a static data member of a class has external linkage if the name of the class has external linkage. [$3.5/5]
  • a static variable inside a function retains its value even after returning from the function. That is, its lifetime is equal to the lifetime of the program itself. This is demonstrated in Mahesh's answer.
查看更多
做个烂人
5楼-- · 2020-07-23 07:05

There are three different types of "static" variables.

  1. Outside functions and classes, a "normal" variable would be a global variable. A static variable outside a function is not global, but local to the .cpp file in which it's defined. (Don't define this type of static variables in header files!)

  2. Inside a function, a normal variable is destroyed when the function exits. A static variable in a function retains its value even after the function exits.

  3. Inside a class, a normal member belongs to an object. A static member is shared between all objects of that type, and even exists before any object of that class is created.

查看更多
在下西门庆
6楼-- · 2020-07-23 07:08

Static variable retains its value during function calls/loops but local variable doesn't;

#include <iostream>

void foo()
{
    for( int i=0; i<5; ++i )
    {
         static int staticVariable = 0;
         int local = 0;

         ++local;
         ++staticVariable;

         cout << local << "\t" << staticVariable << "\n";
    }
}

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

Results:

1 1
1 2
1 3
1 4
1 5

When a static variable is a member of a class, each instance share the static variable. Each instance doesn't have it's own copy.

class foo
{
     public:
     static int staticVariable;
};

int foo::staticVariable = 0;

foo obj1, obj2 ; // Both the instances share the static variable. 
查看更多
Explosion°爆炸
7楼-- · 2020-07-23 07:10
  1. Static members are members that are single shared member common to all the objects created for a particular class but non-static are not so.
  2. Memory allocation is done only once and not every time an object is created like non-static members.
  3. Only one copy of static data member will exist irrespective of the number of objects created.
  4. Static member functions can access only static member variables while a non-static member can be accessed by both static and non-static member functions.
  5. Static members are efficient when single copy of data is enough.
查看更多
登录 后发表回答