I am very depressed, this book really said very detailed, I am looking at chapter II, after reading to take a deep breath, really too detailed (Lao) fine (DAO).
But that's good, and the hard stuff behind it should be good.
So, this chapter just as a preview of the chapter, I take out some slightly more nutritious content to record it ~
Note: This series of tutorials is not suitable for friends without any programming language basis, but as a supplement to the C + + Foundation.
The tutorial content is based on "C + + Primer Plus" a book, Suitable for beginners, skilled please ignore directly.
1. If you don't annotate, then I'll comment you out ~!
There are 2 of annotations in C + +,
The first, one-line annotation, that is://
The following code:
Copy Code code as follows:
Oh
test* t = new Test ();;
followed by is the annotation content, belongs to the Single-line comment
The so-called annotation, is not the code-independent content, and compiler-independent content, just for ourselves or other people to see.
It's like a manual.
There is also a note, that is: */*/
The following code:
Copy Code code as follows:
/*
Oh
What did you just say?
*/
test* t = new Test ();;
/**/can be placed in the middle of a text, this is a multiline comment.
This nonsense is not much to say.
Also to say a bit of nonsense (Jor: who just said the nonsense not much? And that's that annotation is really important ~!
Don't listen to Daniel. The variable name is obtained well, the function name is obtained well and no annotation is needed.
Everyone has a different opinion about "good", so it's important to note, who likes to look at code that doesn't have a whole page of annotations?
Whether it's an English annotation or a Chinese annotation, you can get someone else to know the meaning of a piece of code as quickly as possible.
Yes, if you meet someone who thinks they've written the perfect code and doesn't have a single note, then you're going to have to comment out the whole person. ~!
2. header file
In addition to the grammar, the most important thing in the language itself is the various libraries.
Various libraries, that is, its various functions, we can only call some functions to complete the specific functions.
such as print text, read and write files or something.
C + + provides many of these libraries, which are stored separately in different files.
If you want to use the functions inside these libraries, you should include the header files for those libraries.
So, we often see the following code:
Copy Code code as follows:
#include "hello.h"
#include <iostream>
#include就是用来包含头文件的, a function of a library can be used after the header file is included.
So why do some headers end in. h, and some of them don't?
In fact, now the C + + default rule is not required. h ending,. h ending is just a habit left before
But, in fact, now everyone in the new class file is still used to keep. h, and C + + libraries may have more to do with no suffix.
In fact, the header file is a file, what the end of the relationship is not (most of the case), so sometimes we can see the end of the file. hpp.
This is not much to say ~
In short, if you compile the code when the error, say what can not use undeclared type and so on, basically is not include the corresponding header file.
3. Namespaces
As a beginner, you should often see the following code (JOR: Isn't it common for beginners?) ):
Copy Code code as follows:
#include "Cocos2d.h"
using namespace cocos2d;
For example, we want to use the Cocos2d-x sprite class, which is in the Cocos2d.h library, so we first want to include the Cocos2d.h header file.
Then use the Sprite class, such as: sprite* SP = sprite::create ("Boy.png");
But I believe we have tried, so direct use will be the error, it is said that can not find sprite.
This is because Sprite is in a namespace, and we want to use it this way: cocos2d::sprite* sp = cocos2d::sprite::create ("Boy.png");
So that's no problem, so what is a namespace?
4. The same class name conflict in different libraries
Let's run the question first, suppose I want to contribute, write a better library for everyone (header file called mutou.h), and then, there is a class called Sprite.
So, people will use my library like this: sprite* mSp = sprite::new ("Girl.png");
So, the problem is, learning excavator which home ... (Jor: Stop ~! Come back! Don't get sidetracked! )
So what happens if we want to use both the cocos2d and Mutou sprite classes at the same time? The following code:
Copy Code code as follows:
#include "Cocos2d.h"
#include "Mutou.h"
sprite* sp = sprite::create ("Boy.png");
sprite* mSp = sprite::new ("Girl.png");
This will certainly be an error, probably say Sprite is not clear
So, at this point, you need something called a "namespace" to differentiate between these different libraries of the same name class.
We put the Cocos2d library in the namespace named Cocos2d, and put the Mutou library in the namespace named Mutou:
Copy Code code as follows:
#include "Cocos2d.h"
#include "Mutou.h"
cocos2d::sprite* sp = cocos2d::sprite::create ("Boy.png");
mutou::sprite* mSp = mutou::sprite::new ("Girl.png");
This will not be a problem, as to how to put a library into a namespace is very simple, but here is not introduced.
5. Lazy
Back to the beginning, using namespace cocos2d; What is it again?
As you've seen, always add cocos2d to the front of the sprite:: This prefix is really annoying.
So we can be lazy and use a using namespace cocos2d, and then we don't have to write this prefix.
However, it is only in the absence of conflict that the prefix must be used if you encounter the same class name conflict with the different libraries that you just said.
It is mentioned in the book that misuse of using namespace is not a good thing, but I have not experienced any pits, it may be in large projects will have problems.
The General Hand tour is no effect.
If you are concerned, you can use this: using namespace Cocos2d::sprite;
This can still be used without cocos2d:: Prefixes can use the Sprite class, but if you want to use the COCOS2D Library of other classes, you will again use the using namespace Cocos2d::name;
All in all, this is the kind of class that uses a using declaration. Can save some money.
6. End
Well, the front content is so basic that nothing can be introduced.
The next chapter should be better ...