Namespaces without classes?

Hello Everyone,

So I’m very new to OOP.

I was told once that unless you need to make multiple objects there isn’t a need to use classes and that I should use Namespaces instead when you are just needing to hold some functions. To be honest this really confused me.

For example something like this.
http://www.cplusplus.com/forum/windows/182244/

However here is what I’m confused about. If you are going to make a new namespace don’t you have to use a class files in order to do so?

Sorry total beginner here.

Thank You!
If you are going to make a new namespace don’t you have to use a class files in order to do so?


No, not at all. One can have ordinary variables, functions, POD structs - whatever you like in a namespace. The only requirement is to refer to them correctly, either with the scope operator, a namespace alias, or a using declaration for a type. Namespaces can be nested as well.

This example is rather contrived, but it shows how a namespace can be used to store constants, as one sort of usage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace PhysicsConstants {

  const double Avogadro = 6.02214085774e23;

struct MyConstants {
  const double a = 1.00023;
  const auto b = 2.0456; 
};


}  // end of PhysicsConstants

.
.
.

namespace PhyC = PhysicsConstants;  // namespace alias

std::cout << "Avogadro const is " << PhyC::Avogadro << "\n";

// without alias
std::cout << "Avogadro const is " << PhysicsConstants::Avogadro << "\n";

// using declaration for a type
using mc = PhysicsConstants::MyConstants;

std::cout << "My a  is " << mc::a << "\n";




One doesn't have to put everything into one namespace declaration, I could have done MyConstants like this:

1
2
3
4
5
6
7
8
namespace PhysicsConstants {

struct MyConstants {
  const double a = 1.00023;
  const auto b  {2.0456};  // use auto and brace init rather than double C++11
};

}  // end of PhysicsConstants ns 


Note that using an entire namespace is a bad idea - especially using namespace std; read up about that on Google.

using one thing out of a ns is OK, like using std::cout; , but that becomes tiresome as well, so just use std::cout or std::find or mc::a or whatever it is.


http://en.cppreference.com/w/cpp/keyword/namespace


God Luck !!

Awesome thank You! I'll have to do some playing around with that!
closed account (E0p9LyTq)
RPDStars wrote:
If you are going to make a new namespace don’t you have to use a class files in order to do so?


You could group a lot of related variables and functions into a namespace, having a class or classes in a custom namespace is not a requirement.
@TheIdeasMan

using one thing out of a ns is OK, like using std::cout; , but that becomes tiresome as well, so just use std::cout or std::find or mc::a or whatever it is.


How tiresome is it to type std:: a few hundred times a day?
Thomas1965 wrote:
How tiresome is it to type std:: a few hundred times a day?
It becomes a habit and isn't that tiring. Maintaining a list of using declarations is more tiring (unless your IDE can do it for you).
@Thomas1965

No, no, I meant doing the following which is tiresome (which is what LB is saying as well):

1
2
3
4
5
6
7
8
9
10
11
using std::cout;
using std::cin;
using std::string;
// lots more using declarations

.
.
.

string AString = "Just do std:: \n"
cout << AString;


Regards and Happy New Year to all :+)
Topic archived. No new replies allowed.