Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnamed namespaces vs private variables

Tags:

c++

namespaces

I have been reading through other questions on here and there is something that has me confused and hopefully it can be explained. I am sure there it is a simple thing but it is alluding me.

So in C++ we have private variables that are only viewable within the class:

class MyClass
{
    private:
        int i;
};

But we can also have unnamed namespaces:

namespace
{
    int i;
}

Both appear to be private to the class but in the 2nd case you cannot see they exist from the header file. From reading other questions it seems that functions are different as you can't pass class objects to them? But I am not sure what the difference is here for variables.

Is there a disadvantage to the 2nd way that means you should still use private variables?

like image 711
Firedragon Avatar asked Aug 31 '12 10:08

Firedragon


People also ask

What is the purpose of unnamed namespace?

An anonymous namespace makes the enclosed variables, functions, classes, etc. available only inside that file. In your example it's a way to avoid global variables. There is no runtime or compile time performance difference.

What is true about an unnamed namespace?

Unnamed NamespacesThey are directly usable in the same program and are used for declaring unique identifiers. In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace. The name of the namespace is uniquely generated by the compiler.

Can a namespace be private?

An object namespace protects named objects from unauthorized access. Creating a private namespace enables applications and services to build a more secure environment. A process can create a private namespace using the CreatePrivateNamespace function.

Why use private variables instead of public?

Class variables that are declared as private can not be referred to from other classes, they are only visible within their own class. It is considered better programming practice to use private rather than public class variables, and you should aim to do this in the remainder of the course.


2 Answers

They aren't the same.

Integer i in the anonymous namespace will be shared by all instances of MyClass.

The private integer i in MyClass will be unique for each instantiation of the class.

The equivalent using private would be to make i static:

//.h
class MyClass
{
    private:
        static int i;
};

And instantiate the one single shared i like this:

//.cpp
int MyClass::i = 0;
like image 117
Peter Wood Avatar answered Jan 04 '23 12:01

Peter Wood


Both appear to be private to the class ...

No, only the first is private to the class. It's a non-static member variable; one is instantiated in every object of the class type.

The second is not in a class at all; it has static storage duration, so one is instantiated for the whole program. Anything that accesses it is accessing the same variable as anything else that accesses it. Being in an unnamed namespace, it's only accessible within the translation unit (i.e. the source file) that defines it; but it's accessible to any code there, not just a particular class.

Is there a disadvantage to the 2nd way that means you should still use private variables?

If you want a copy of the variable in each class object, then you need it to be a non-static member.

If you want to share it between all objects, then it's up to you whether to make it a static member, or put it in a namespace inside the class's implementation file. I often do the latter to simplify the class definition. Disadvantages are that access isn't restricted just to the class but to anything else in that file, and you can't access it from any code that you might want to put in the header.

like image 24
Mike Seymour Avatar answered Jan 04 '23 13:01

Mike Seymour