Sign in to follow this  
skydave

define class in global namespace from within another namespace

Recommended Posts

Hi, I would like to define a class in global namespace from within a application namespace like so:
namespace test
{
    namespace ??? // heres my problem: I want to 'junp out' of
                  // the current namespace into the global namespace
    {
        class GlobalClass
        {
        };
    }    
}

GlobalClass c; // should be possible because GlobalClass has been
               // defined in global namespace


Is this somehow possible? Thanks in advance, David

Share this post


Link to post
Share on other sites
I don't understand the question. You can open and close a particular namespace any number of times, so you can just do this:

namespace test
{
// declarations
}

class GlobalClass
{
// ...
};

namespace test
{
// more declarations
}

GlobalClass c;


Share this post


Link to post
Share on other sites
Hi rip-off,

thanks for your reply. Consider the following situation:


namespace test
{
class Object
{
};

REGISTER_CLASS(Object,"Object") // this macro is used to build a class
// registry at compile time
}




Now REGISTER_CLASS is defined as follows:


#define REGISTER_OBJECT(classtype, classname, basetype)BOOST_CLASS_EXPORT_GUID(classtype,classname)\ // compiler traps here
class classtype##Meta
{
public:
classtype##Meta()
{
boost::serialization::void_cast_register(static_cast<classtype *>(NULL),static_cast<basetype *>(NULL));
}
}##classtype##_is_registered;



The compiler traps at the line with BOOST_CLASS_EXPORT_GUID because the macro is used in the namespace test. For simplicity and usability reasons I would like to be able to 'wrap' a global namespace around the macro so that it works. I know that I could use the macro after closing the namespace test, but then I run into other issues which arise from defining the classtype##Meta out of the test namespace (in the real situation the classtype##Meta classes are more elaborate).

Thanks for your time.
David

Share this post


Link to post
Share on other sites
How about this:

namespace test
{
class Object
{
};
}

REGISTER_CLASS(test::Object,"Object") // this macro is used to build a class
// registry at compile time

I haven't really taken the time to understand what you're trying to do, but it seems like this should work.

Edit: No, that won't work I see now. This will try to create a class beginning with test::Object, which is illegal...

Share this post


Link to post
Share on other sites
Quote:
Original post by Perost
How about this:

namespace test
{
class Object
{
};
}

REGISTER_CLASS(test::Object,"Object") // this macro is used to build a class
// registry at compile time

I haven't really taken the time to understand what you're trying to do, but it seems like this should work.

Edit: No, that won't work I see now. This will try to create a class beginning with test::Object, which is illegal...

Use a typedef at global scope then

namespace test
{
class Object
{
};
}
typedef test::Object Serialise_object;
REGISTER_CLASS(Serialise_object,"Object")

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this