C++ basic introductory tutorial (ii) : data variables macros etc

  • 2020-04-02 02:53:54
  • OfStack

Ah, wood head, the book is too detailed ~ tired
The previous section is too basic, and I won't repeat many of the grammar methods
Today I looked at some of the basic points that C++ touches on data, and I took the slightly more nutritious part out of the book

1. Initialization

In general, we declare and initialize variables like this: int iNum = 10;
This is actually a continuation of the syntax of C, and there's another syntax of C++ that goes like this: int iNum(10);
Most people are used to the first one (so am I), but don't be surprised if we see the second one in someone else's code

2. A macro definition

Macros, to put it in the crudest, no, most colloquial terms, are search substitution.
For example, define a macro like this: #define NAME "mutou"
So, we're going to be able to use the NAME macro everywhere, and when the program compiles, it's actually going to replace all the names with mutou, and that's the idea of lookup substitution.
 
Why do that? Easy to modify and maintain, as long as we slightly change the macro definition, like this: #define NAME "psycho"
So everything that USES the NAME changes.

3. Representation of hexadecimal Numbers

Int iNum = 10;

You know, 10 is 10. Xiao if: hey ~ dad, help me prepare 1 million, I want to stab a person to death.
Wait, don't get excited!

10 represents the decimal 10, but we can actually use octal and hexadecimal to express, for example:


int iNum = 012;  //Octal
int iNum = 0XA;  //Hexadecimal

If you put a 0 in front of a number, you're in octal.
If you put 0X in front of a number, it's hexadecimal.
(note that it is the number 0, not the letter o, make no mistake ~)

So don't put a zero in front of a number just to be cool. You don't think 007 and 7 are the same number. (if: octal 7 is the same as decimal 7!)

Well, ahem, so don't put 0X in front of a number just to be cool. You don't think 0X7 and 7 are the same number. (if: hexadecimal 7 is the same as decimal 7!)

Anyway, don't put a 0 or 0X in front of a number unless you know what you're doing.

4. The const

I'm sure you'll see const a lot, right?
Const int iNum = 10; const int iNum = 10;
By this definition, we can no longer assign a value to iNum, which means that iNum is a constant.
 
Also, even if you want to use it this way: const int iNum;
No, we have to initialize the constant
 
There's more to const, but let's stick to the pace of the book, and this chapter is only a passing reference.

5. Auto statement

I'm sure you'll often see the word auto when you learn cocos2d-x3.x.
In fact, this is a new feature of C++11, which can automatically determine the variable type, without requiring us to specify.
If you are familiar with lua, then it can be vulgarly, no, colloquially understood as local.
 
Such as:

Auto iNum = 10; Int iNum = 10; It's the same thing.
Auto sp = Sprite: : create (); Sprite* sp = Sprite::create(); It's the same thing.
 
However, it is best not to abuse, if the type of some variable is not very obvious, it may be more difficult to read and maintain with auto.
And int iNum = 10; You'd better not use auto for this one

End of 6.

The next chapter is more... Hopefully there will be another tutorial soon...


Related articles: