Accessing types outside namespace?

N

none

I have this code:

namespace second {
class Processor {
public:

};
}


namespace first {
namespace second {
class Factory {
public:
void setup(){
Processor processor;
}
};
}
}




int main() {
// will not compile!
first::second::Factory factory;
return 0;
}





Is there any way to instansiate 'Processor' in the class 'Factory' without having to change (adding
the outer namespace 'first') Processor to:


namespace first {
namespace second {
class Processor {
public:
};
}
}


??
 
N

none

Sam said:
That should be ::second::processor processor.

Thanks but it looks a bit ugly. Is this an example of bad design or is that kind of notation often used?
 
P

Paul Bibbings

none said:
Thanks but it looks a bit ugly. Is this an example of bad design or is
that kind of notation often used?

Were you perhaps expecting that your namespace first::second `re-opened'
your ::second? Whether it's good or bad design depends upon what your
requirements are and what your reasons are for structuring your
namespaces as you have done. Essentially, the global scope resolution
operator is required here because of the two distinct uses of the same
namespace name, and the fact that a nested-name-specifier is required at
all depends upon Processor being in a different namespace to Factory.
From a design point of view, you will have to answer whether this is
appropriate or not.

Note that there are options for not having to repeat this `ugly' syntax,
if it is required in many places throughout a broader series of
declarations/definitions in namespace first::second. This can be
achieved, for example, with:

namespace first {
namespace second {
using namespace ::second;
class Factory {
public:
void setup() {
Processor processor;
// unqualified use of other names from ::second
}
};
// ditto
}
}

Regards

Paul Bibbings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top