Hi all,

I am quite confused about the usage of both. Both of them seems to enable a variable to be shared consistently between classes.

Is the following true?
Namespace-used for defining some constant values that you want to share in your application.
Static variable-used to maintain a consistent value throughout the application.

Does variables in a namespace behaves like a static variable where it will keep the most recent value?

Any help is much appreciated.

Cheers!

Yes, the information is correct, but it isn't explaining much (e.g: a namespace is not only used to share some constant values) ...

Namespaces:
e.g. a variable called 'animal' declared in namespace A won't conflict with a variable called 'animal' declared in namespace B ...

Static variables are often used in classes (because you can share the same variable among all the objects created from the class) ...
e.g. If you want to count the number of instances a class has, without having to use a global variable ...
A static variable is stored in the static memory of your computer, static variables are also used sometimes in functions: normally all the function's local variables are lost when a function exits, but if you declare a static variable in a function, the variable won't be gone after the function has exited, a static variable stays in memory until the program exits

I hope I explained everything correctly and in a way you can understand it ...
If you still have questions, please feel free to ask ...

Edit::
-> http://www.cprogramming.com/tutorial/namespaces.html
-> http://www.cprogramming.com/tutorial/statickeyword.html

Probably if you try searching Google you'll find some other information about it ...

>>Does variables in a namespace behaves like a static variable where it will keep the most recent value?

No. static variable may appear either inside a class or outside any class.

  • When static variables are part of a class then they require class scoping, such as MyClass::MyVariable = 0; They must also be declared globally just like any other global variable with the class scope just as in the previous example.
  • When static variables are declared globally (not inside a class) then they are in global namespace unless you put a namespace around them.
namespace mynamespace
{
    int MyGlobal = 0;
}

Functions and methods can also be declared static.

commented: I let that question open for you :P +1

So to summarize, all static variables are actually globally scoped reason being that they are initialised outside the classes?

For namespaces, the variable are locally scoped since we need to specifically define if we want to use it.

Another question is that if i have 2 classes using the same namespace(both includes a file that defines a namespace), will there be one copy of the namespace variable for each of the classes or will there only be one copy shared by both of them?

Thanks for all help!

If you have a static variable in each of those two classes then they will be distinctly different variables.

class A
{
public:
    static int x;
};

class B
{
public:
    static int x;
};

A::x and B::x are two different variables

an example in a program would be somtin like:

What would the example look like ? :)

Member Avatar for stephen.id

oops lol crap, i accidently closed the browser, i didnt save the code either, it was just some code that outputs the numbers from the class :P

[B]mynamespace.h[/B]

namespace mynamespace
{
    int x=0;
}
--------------------------------------
[B]A.h[/B]

#include <mynamespace.h>
using namespace mynamespace
class A
{
public:
     void setX() { x=1 }
      int getX() { return x}
};
--------------------------------------
[B]B.h[/B]
#include <mynamespace.h>
class B
{
public:
    int getX() { return x}
};
---------------------------------------
#include <A.h>
#include <B.h>

int main(int argc, char* argv[])
{
    B b;
    A a;
    a.setX();
    cout << b.getX() << a.getX() << endl;
}

What will be the output? Are the classes sharing the same x?

Sorry as i do not have a c++ compiler at my home, i couldnt try it out now. But i would like to clear my doubts on the concepts this weekend.

Thanks for all the replies!

Member Avatar for stephen.id

after some code editing i managed to get this :P:

#include <iostream>

using namespace std;

namespace SetNumberX { int X = 0; }
namespace SetNumberY { int Y = 0; }

using namespace SetNumberX;
using namespace SetNumberY;

class NumberX {
    public:
        int SetX(int CX) { return X+CX; };
        int GetX() { return X; };
};

class NumberY {
    public:
        int SetY(int CY) { return X+CY; };
        int GetY() { return Y; };
};


int main() {
    NumberX X;
    NumberY Y;
    int GX = X.SetX(10);
    int GY = Y.SetY(20);
    cout << "X: " << GX << endl;
    cout << "Y: " << GY << endl;
}

To the OP: #include <mynamespace.h> has to be #include "mynamespace.h" (you've to change it everywhere in your code unless 'mynamespace.h' is in your compiler's directory of include files, but I think that isn't the case)

in A.h void setX() { x=1 } has to be void setX() {x=1;} int getX() { return x} has to be int getX() {return x;} in B.h int getX() { return x} has to be int getX() {return x;} in your main program #include <A.h> #include <B.h> has to be #include "A.h" #include "B.h" (unless these header files are in your compiler's directory of include files, but I think that isn't the case)

But as already mentioned you haven't access to a compiler to test this :) ...

mynamespace.h

namespace mynamespace
{
    int x=0;
}

Don't put that in a header file because you will get duplicate declaration error messages!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.