Reputation: 14299
Relatively new to cpp. When you subclass a baseclass, the #include cascade to the subclass, why don't the class-wide usings in the cpp file also cover the scope of the subclass? Is this a historical or pragmatic reason? Err.. What is the reason?
//available to subclass
#include <cinder/app/appBasic.h>
// have to duplicate in subclass
using namespace ci;
using namespace ci::app;
using namespace std;
Upvotes: 0
Views: 125
Reputation: 279285
The main reason is that the compiler only looks at one .cpp
file plus whatever it #includes
. It has no idea what using
statements there might be in the .cpp
file for your base class. For that matter, it has no idea whether you've even written the .cpp
file for the base class yet when you compile the .cpp
file for the derived class. And it's not going to go rooting around the filesystem to find out, unlike for example javac
.
Furthermore, I guess you're writing one .cpp
file per class, and giving the file a name that has something to do with the class name. But C++ doesn't require that. You can have more than one class in a file, or for that matter you could split a class across multiple files if you want to.
So, you know that this .cpp
file is the file for your derived class, and that the other .cpp
file is the file for the base class, and therefore you think it might be convenient if some stuff from the other .cpp
file was lifted into this .cpp
file. But the compiler knows no such thing. It wouldn't make sense to the C++ compiler to talk about what's in the file for the base class.
Finally, a reason that is principled rather than pragmatic: just because it is convenient for the implementer of the base class to bring the names from certain namespaces into global scope doesn't mean the same will be true of the implementer of the derived class. The derived class might not use ci::app
at all, let alone use it so much that the person writing the derived class is sick of typing it. So even if C++ could require the compiler to fetch those using
statements (which, given the compilation model, it can't), I'm pretty sure the language's designers wouldn't want it to.
the
#include
cascade to the subclass
No they don't. Any #include
in the base.h
will be included into derived.cpp
if (for example) derived.cpp
includes derived.h
which includes base.h
. But any includes in base.cpp
have no effect on derived.cpp
.
This is all assuming that you follow the usual naming convention. There's nothing other than convention to stop you including a .cpp
file from derived.cpp
, in which case (1) any includes or using
statements would apply in derived.cpp
, but unfortunately (2) you'll probably break your build system, because most likely you won't be able to link base.o
and derived.o
together any more on account of them containing duplicate definitions for code entities subject to the One Defintion Rule. That is, functions.
Upvotes: 1
Reputation: 11047
using directive
and using declaration
are only valid for the current translation unit. you can put these in the header file which is not a good practice.
Upvotes: 1