Why in C++ do I need to use namespace after already using import directive?
In C# one only uses an using directive for a namespace. In C++ like in this example below, one must do an include then use a namespace, why 开发者_如何学运维isn't it the same thing like in C# ?
#include <iostream>
int main()
{
using namespace std;
cout << "Hello world!" << endl;
return 0;
}
Because the compiler in C# works very differently than C++.
The quick version - C# uses references in a (vaguely) similar fashion as C++ uses the linker, and forward declarations. C++ requires forward declarations so it can compile each source file to a translation unit, then link all those units together. The C# compiler loads the code for your referenced assemblies, then compiles your whole assembly at once, and therefore doesn't need those sorts of forward declarations.
The long version -
C++
In C++, if you want to use code, you have to add it to the linker, and provide forward declarations. Header files contain forward declarations, and include files simply copy/paste that file into your source file.
This is because C++ has a mid-step called translation units, and produces object files. There are glued together by the linker.
In your example, #include <iostream>
provides the forward declarations, and does so by including a header file (previously called "iostream.h", but now called "iostream"). This header file gets copy/pasted into your source code (along with all the headers that file includes, and so on). This all gets compiled down to a translation unit (object file), linked to the C++ runtime (as well as probably the C runtime, and maybe others), and turned into an executable.
C#
In C#, if you want to use code, you add a reference to the project or build file, or you simply add the C# file to the same project. No forward declarations are required, but you do have to reference the code, or have another source file in your current assembly that contains the code.
C# doesn't have the translation unit mid-step. It only produces assemblies.
When you reference System.dll, you already have the code available to you. You don't have to provide forward declarations, and therefore don't need to include any headers.
Both
You can use the using
keyword to either pull an entire namespace into your current scope. In C++, you invoke using namespace std;
. In C#, you invoke using System;
Then you can do things like:
std::cout << "test1\n";
cout << "test2\n";
Or
System.Console.WriteLine("test1");
Console.WriteLine("test1");
I forget if you can in C++, but in C# you can also alias namespaces:
using Ser = System.Runtime.Serialization;
...
class Blah : Ser.ISerializable {
Or alias types:
using ISer = System.Runtime.Serialization.ISerializable;
...
class Blee : ISer {
I normally wouldn't answer a question with so many useful answers, but nobody seems to have noticed "import" in your question. In C and C++, the "#include" directive is not the same as "import" in Java or "using" in C#. Literally, a #include directive simply adds lines you the compilation of the file containing the directive. Period.
There is no comparable feature to #include in most other compiler languages; but there is such a feature in nearly every useful assembler. C is a low-level language originally designed for portable implementation of operating system kernels, drivers and utilities. C++ is a higher-level language, but is built upon C. A deliberate design goal was to allow most C programs to compile with no source changes as legal C++ programs.
Keep that picture in mind as you study C++. It may help you understand how why it's put together the way it is.
The C++ standard library is much smaller than the frameworks for Java or C#, and all of it is defined within the std namespace. That means that the only "import" you need for the entire library is the "using namespace std;", which is what brings those names into view without explicit std:: prefixes; but only those names that have been #included into the current compilation.
If you don't use an include, then as far as the compiler is concerned, whatever is declared in that header doesn't exist. In order to use a function, the compiler must know that it exists. The using directive and the #include are totally tangental- you don't have to use a using at all and many more experienced programmers recommend against it.
the include directive just copy-pastes the source file into your progams, nothing more (in your code, for example, #include puts the source-file "iostream" that's in your C++ compiler install into your source code.
The using direcive, on the other hands, does exactly what the one in C# does.
The purpose of using namespace std; line is so you don't have to type out the full std::cout, std::endl every time. It will assume when you do use cout, endl, or any other function from the standard library without the std:: that you wanted to use the one from the standard library.
You can try this yourself, remove using namespace std; and try to compile. You should get errors. Then try to compile again using std::cout << "Hello world!" << std::endl; and it will work like before.
Check this out here:
http://www.cplusplus.com/doc/tutorial/namespaces/
It actually is similar in C#. Using is indeed only for namespaces, even in C# Try using something from an assembly you haven't referenced, it won't work no matter how many times you say using Foo.Bar
. Adding the reference is similar to the include
step in C++ (although not identical, C# makes "including" and "linking" a single step).
精彩评论