static initialization confusion

Posted by Happy Mittal on Stack Overflow See other posts from Stack Overflow or by Happy Mittal
Published on 2010-06-14T10:26:59Z Indexed on 2010/06/14 10:32 UTC
Read the original article Hit count: 186

Filed under:
|
|

I am getting very confused in some concepts in c++. For ex: I have following two files

//file1.cpp
class test
{
    static int s;
    public:
    test(){s++;}
};

static test t;
int test::s=5;

//file2.cpp
#include<iostream>
using namespace std;
class test
{
    static int s;
    public:
    test(){s++;}
    static int get()
    {
    return s;
    }
};

static test t;

int main()
{
    cout<<test::get()<<endl;
}

Now My question is :
1. How two files link successfully even if they have different class definitions?
2. Are the static member s of two classes related because I get output as 7.

Please explain this concept of statics.

© Stack Overflow or respective owner

Related posts about c++

Related posts about static