Ken Fitlike says
Another option is to use an unnamed namespace.
well,
this is my code, it is working fine with static.
but i would like to know how to do it using unnamed namespace.
//// mainfile.cpp
Code:
#include<iostream.h>
void ff();
void ff2();
void main()
{
cout<<" inside the main\n";
//i=200;
//cout<<"\n value of i is "<<i<<"\n";
ff();
ff2();
}
//// secondfile.cpp
Code:
#include<iostream.h>
static int i=100;
void ff()
{
cout<<"\n inside the ff() \n ";
i=500;
cout<<"\n value of i is "<<i<<"\n";
}
void ff2()
{
cout<<"\n inside the ff2() \n ";
i=600;
cout<<"\n value of i is "<<i<<"\n";
}
************************
i got it
//// secondfile.cpp
Code:
#include<iostream.h>
namespace {
int i=100;
}
void ff()
{
cout<<"\n inside the ff() \n ";
i=500;
cout<<"\n value of i is "<<i<<"\n";
}
void ff2()
{
cout<<"\n inside the ff2() \n ";
i=600;
cout<<"\n value of i is "<<i<<"\n";
}
This works fine,
but i wonder how variable i is accessible in the function ff() & ff2() in the file secondfile.cpp
????????????
*********************
i got it again
Unnamed namespaces
Each translation unit contains an unnamed namespace that you can add to by saying namespace without an identifier:
namespace
{
class Arm { /* ... */ };
class Leg { /* ... */ };
class Head { /* ... */ };
class Robot
{
Arm arm[4];
Leg leg[16];
Head head[3];
// ...
} xanthan;
int i, j, k;
}
int main()
{
}
The names in this space are automatically available in that
translation unit without qualification. It is guaranteed that an
unnamed space is unique for each translation unit. If you put local
names in an unnamed namespace, you don’t need to give them
internal linkage by making them static
Thanks to all for help.
I learnt a new thing