Name spaces in classes.

Hello!
I have two functions that are called the same but pull in different info. How do I use using namespaces in a class? I know how to do it with a function but now within a class. Can anyone give me an example?

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
class numbers
{
public:
    .
    .
    void retrieve (int x, int y);
    void retrieve (int y, int z);

private:
    int a;
    int b;
    int c;
};
Sounds to me like the should simply have different names.
I saw that comment coming from a a while away. Ok so let me modify this question.... If speficications required me to use the functions, how would i go about using namespaces. I understand that you can simply change the names but for this purpose im learning how to use namespaces.
1
2
3
4
5
6
namespace abc {
  void retrieve(int,int) { cout << "namespace abc\n"; }
}
namespace def {
  void retrieve(int,int) { cout << "namespace def\n"; }
}


I don't think we can have namespace WITHIN a class correct ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace abc {
  class numbers {
    public:
      void retrieve(int,int) {}

  }
}

namespace def {
  class numbers {
    public:
      void retrieve(int,int) {}

  }
}

Unfortunately, this code does not compile:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Vector
{
	int x, y;
public:
	namespace X
	{
		int Get(){ return(x); }
	}
	namespace Y
	{
		int Get(){ return(y); }
	}
};
It gives a syntax error that 'namespace' is invalid.
Last edited on
The name of a thing matters. Along with its type, a thing is uniquely identifiable. In all programming languages, C++ included, you cannot name and type two things identically. Were you able to do that, the language would become non-deterministic.

Imagine having identical twin children, and naming them "Alice". How would you distinguish them? You could not, unless you were to do something to make them different. (Alice with dyed-red hair. Or Alice Ann and Alice Lynnette.)


A related concept, which I think you are confusing here, is called function overloading, where you give two things of different types the same name.
1
2
void doit( unsigned    x );  // overload 1: takes an integer argument
void doit( std::string s );  // overload 2: takes a string argument 
The compiler can tell which of these you mean by simply looking at the type of argument you give it:
1
2
doit(  17  );  // that there is an integer -- you must want overload 1
doit( "42" );  // that there is a string -- you must want overload 2 



Another related concept is namespaces. Namespaces were created to avoid name collisions among library implementors.

Suppose company X and company Y both implement nice compression algorithm. They are different algorithms, but both companies want, naturally, to name their compression functions "compress" and "decompress".

Now suppose that you want to write a little program that can manipulate compressed files using either method. You #include the headers for each companies libraries, compile, and ... the compiler and/or linker complains that you have two different things with the same name and type.

In order to overcome this problem, companies X and Y are both smart enough to wrap their functions in namespaces.
1
2
3
4
5
6
7
8
9
10
11
12
// company X's awesome compression routines
//
namespace company_x
  {
  template <typename InputIterator, typename OutputIterator>
  OutputIterator
  compress(
    InputIterator  first,
    InputIterator  last,
    OutputIterator result
    );
  }
1
2
3
4
5
6
7
8
9
10
11
12
// company Y's excellent compression routines
//
namespace company_y
  {
  template <typename InputIterator, typename OutputIterator>
  OutputIterator
  compress(
    InputIterator  first,
    InputIterator  last,
    OutputIterator result
    );
  }
Now, as you can imagine, you can choose which compression library to use because the routines both have different fully-qualified names. Alice Ann and Alice Lynnette at work:
1
2
3
4
if (user_wants_compression_x)
  company_x::compress( source.begin(), source.end(), ostream_iterator <char> ( myfile ) );
else
  company_y::compress( source.begin(), source.end(), ostream_iterator <char> ( myfile ) );
Obviously, there is no confusion over which company's compressor is used, because they have different names.


All these things work on different levels. As already indicated, namespaces work on the global, encapsulating level. Function overloading works within that outer level.


Finally, a note about logical organization. Just as the Alices were confused because they were two different people with the same name -- you should not confuse two different functions by giving them the same name. The name of a thing is a strong indication of what that thing is/does. (You can safely assume that Alice is a girl.)

If you have a function "Get" that returns some random thing, how is that useful? You must be explicit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Vector
{
        int x, y, z;
public:
        int GetX() const { return x; }
        int GetY() const { return y; }
        int GetZ() const { return z; }
        int Get( int index ) const
        {
                switch (index)
                {
                        case 0: return x;
                        case 1: return y;
                        case 2: return z;
                }
                return 0;
        }
};

Hope this helps.
Topic archived. No new replies allowed.