May 05, 2024, 07:31:52 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


What is the importance of namespaces?

Started by Parker, January 10, 2006, 02:00:20 PM

Previous topic - Next topic

0 Members and 5 Guests are viewing this topic.

Parker

I know that C++ and C# support namespaces and java calls them packages I think, if they're the same thing. But I was wondering, what are they and what's the point? Are they just an alias for a class, or is there more?

Parker

And for anyone that knows a lot about this stuff, reinterpret_cast, static_cast, const_cast, dynamic_cast? Sorry, it's just I'm not used to what those do and I can't imagine how you can cast in that many different ways.

Zen

Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

For example.

#include <iostream>
using namespace std;

namespace first
{
  int var = 5;
}

namespace second
{
  double var = 3.1416;
}

int main () {
  cout << first::var << endl;
  cout << second::var << endl;
  return 0;
}


As for the rest Parker, i have no idea.

Lewis

Parker

That's interesting, you can do the same by using static variables in classes. Maybe it's just one of those extra features they thought would be a good idea.

Ionic Wind Support Team

The multiple forms of casting are needed with C++ since you can derive an object from more than one base class at once. 

class Complex: base1, base2
{
}

If you had a pointer to the class at the level of base1, which is common when using lists, and you want to access a method in base2 you need to reinterpret the case.  Which is only safe ofcourse if all objects contained in the list derive from the same two base classes.

Paul.
Ionic Wind Support Team