I have found that you can sort of mimic the c# namespace like this;
namespace ABC_Maths{
class POINT2{};
class Complex{};
}
namespace ABC_Maths_Conversion{
ABC_MATHS::Complex ComplexFromPOINT2(ABC_MATHS::POINT2)
{return new ABC_MATHS::Complex();}
ABC_MATHS::POINT4 POINT2FromComplex(ABC_MATHS::COMPLEX)
{return new ABC_MATHS::POINT2();}
}
namespace ABC
{
}
But code doesn't seem to be very tidy. and i would expect to have long winded usage
It is better to nest as much functionality into classes something like
namespace ABC{
class Maths{
public:
class POINT2{};
class Complex:POINT2{};
class Conversion{
public:
static Maths.Complex ComplexFromPOINT2(MATHS.POINT2 p)
{return new MATHS.Complex();}
static MATHS.POINT2 POINT2FromComplex(MATHS.COMPLEX p)
{return new ABC::MATHS.POINT2();}// Can reference via the namespace if needed
} /*end ABC namespace*/
And that is still a little bit long winded. but does feel a little bit OO.
And hears how it seems best done
namespace ABC
{
class POINT2{};
class Complex:POINT2{};
Complex ComplexFromPOINT2(POINT2 p){return new Complex();}
POINT2 POINT2FromComplex(Complex){return new POINT2();}
}
hears how usages would look
int main()
{
ABC_Maths::Complex p = ABC_Maths_Conversion::ComplexFromPOINT2(new ABC_MATHS::POINT2());
// or THE CLASS WAY
ABC.Maths.Complex p = ABC.Maths.Conversion.ComplexFromPOINT2(new ABC.Maths.POINT2());
// or if in/using the ABC namespace
Maths.Complex p = Maths.Conversion.ComplexFromPOINT2(new Maths.POINT2());
// and in the final case
ABC::Complex p = ABC::ComplexFromPOINT2(new ABC::POINT2());
}
its been interesting to find out why i never used c++ namespaces like i would with c#. It would be too long winded, and would never work the same way as c# namespaces.
the best use for namespaces in c++ is to stop say my super cout function (that dings every time its called) from getting mixed up with the std::cout function (which is far less impressive).
Just because c# and c++ have namespaces, it doesn't mean that namespace means the same thing. they are different but similar. The idea for c# namespaces must have come from c++ namespaces. somebody must have seen what a similar but different thing could do, and didn't have enough imagination power left to give it its own original name like "ClassPath" which would kind of make more sense as its a path to classes rather than for providing naming spaces where each space can have the same names
I hope this helps someone
I forgot to say That all of these ways are valid, and moderate use of the first two can be incorporated into the the third to create a library that makes sense ie(not great example)
_INT::POINT2{}
and
_DOUBLE::POINT2{}
so you can change the precision level by using
#define PREC _DOUBLE
// or #define PREC _INT
and then creating an instance of PREC::POINT2 for double precision POINT2
That is not an easy thing to do with c# or java namespaces
obviously that's just one example. Think about how the usage read.