Using namespace std

Pages: 12
As I (somewhat but not too successfully in attempted humor) tried to say, "class space" is a made up term I don't intend to acknowledge beyond an attempt to say that a class encloses functions, other classes (rarely) and data.

This is part of name mangling. If "foo" is a member of class "A", the actual mangled name of the function is "A::foo" by whatever specific mangling convention a compiler uses (might be, more literally, a_foo, or something akin).

A namespace prepends a mangled element, so that if "A" were in namespace "M", the name would actually be M::A::foo, and the mangled version might be M_A_foo, or something to that effect.

A class is not a namespace - it runs under different rules, but the name mangling of the members of a class does loosely resemble the mangling of names within namespaces.

Before namespaces, we often used static functions in classes to achieve the effect. The specifics of what is allowed differs, such that, as we just pointed out, a using namespace declaration can't be inside a class, but it can be inside another namespace.

So a class does not act like a namespace in all respects, only loosely in the manner by which mangled names are generated.

What I've termed (and I regret deeply by now) a "class space" is more classically known as class membership or encapsulation.


Last edited on
So namespaces are a form of "name mangling" and classes can be used to a certain extent to mangle certain things like functions, but it’s a tad bit unconventional..

So I could do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <bitset>

class BN{
  public:
  static add(int a,int b){
    return a + b;
  }
};

int main(){
  std::cout << BN::add(5,6);
}
Well....maybe

static int add( int a, b )...

There must be a return type to use return, but otherwise that is the classic use of a static member function.

It's name is manged with BN, it is a member of BN, it runs without an instance of a BN.

In many respects it looks like a non-member function in a namespace.

Ok, I think I understand it now, thanks! (Sry about the missing return I was spaced)
I prefer, over namespace, but sometimes std, and definitely typedef.


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
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::getline;
using std::istream;
using std::ifstream;
using std::endl;

int main()
{
  //for 0 to 10 ++S_A must be 10, and while must be 0 
  //for 10 to 0 --S_A must be 0, and while nust be 10
	int S_Array(10);
	while(S_Array >= 0)
	{
		//Default = S_Array;
	cout<<"Default Array now is "<<S_Array<<endl;
	 --S_Array;
   
		
  }
	      
    return 0;
}



Example
Last edited on
Topic archived. No new replies allowed.
Pages: 12