I am writing a library with mutiple dependent modules. When I include a file from a different module, should I resolve the namespace with:
using namespace project1::namespace1;
class1 obj;
or
typedef project1::namespace1::class1 class1;
class1 obj;
What are the pros/cons of each approach? I read somewhere that we should use typedef in .H files and using in .C files, is this advisable?
One problem I have encountered with ‘typedef’ is it leads to namespace ambiguity if I include both original class and the class with ‘typedef’ in a third module.
The two options you state are not equivalent. This one:
pulls in everything from the namespace, giving you little control and making clashes likely. I see only cons, and no pros here.
But you don’t need to use a
typedefto bring in a single symbol, you can useWhether you use this or the
typedefdoesn’t make too much of a difference. But bear in mind thattypedefis limited to types and enumerations, whereasusingcan refer to values, functions, etc:so the two expressions are not completely equivalent.