C++ basics tutorial (7) : some of the more specific basic syntax summary

  • 2020-04-02 02:59:03
  • OfStack

This time it's time to get down to basics, introducing some basic loop control, relational expression, logical control, and so on.
Here is a simple draw some slightly more special content to blow water, no, is to summarize it ~

I ++ and ++ I

No, I'm not here to explain the difference between I ++ and ++ I, but one thing the book says is, when you use the for loop, is there a difference between I ++ and ++ I?
The answer is: almost none, whichever you choose.
 
However, a little attention should be paid to the internal handling of the two methods:

I ++ : make a copy of I, add I to 1, and return a copy of I.
++ I: add I to 1 and return I.
 
That said, everyone knows who's more efficient, but with today's compilers, it's automatically optimized (I don't know if that's true for all compilers).
Then, C++ allows you to customize the behavior of the operators, which means that you can define ++ operations for your own classes, where the cost of copying a copy of the class is obviously not negligible.

I won't go into that.

Comma operator

The comma operator can put multiple statements together, such as:


    for (int i = 2, j = 10 * i; i < 999; i++) {
        cout << j;
        break;
    }

Int I = 2, j = 10 times I; That's the comma expression, so what is the value of j? That's right, it's 20.
Again, like this:

int num = 0;
num = 10, 50;

Note that you can't use a comma expression when you declare a variable to be assigned to it. You'll know the rule by yourself when you compile it in the compiler. There's no need to memorize the rule
What is the value of num here? Yes, it's 10, because the assignment is higher than the comma expression, so the 10 is given to num first, and the 50 is discarded.
So, how about this: num = (10, 50);   The result is 50, because the parentheses have a higher precedence than the assignment statement, and the comma expression is glazed, so you end up assigning 50 to num.
 
However, if anyone USES such an assignment in a non-exceptional case, then I promise not to kill him.

Iii. Type alias (typedef)

This is also a pain point for the novice. Most of my friends who are in contact with cocos2d-x for the first time must have been scared to death by these two statements:


typedef void (Ref::*SEL_SCHEDULE)(float);
#define schedule_selector(_SELECTOR) static_cast<cocos2d::SEL_SCHEDULE>(&_SELECTOR)

What is this godlike complexity? This is actually what we're going to use when we use the schedule timer function.
I'll leave it there.
 
Let's start with the simplest use of a typedef:


typedef int IQ;
IQ mutouIQ = 250;
int normalIQ = 1;

A type alias is a nickname given to the name of a type, such as IQ instead of int, as shown in the code above.
The use of a typedef int IQ means that an int is given an individual name, called IQ, which can then be used to define an integer.
In crass terms, IQ is int, and int is IQ
 
Now look at this: typedef char* mpointer;
Then, we can use mpointer to define Pointers of type char later.
 
Did you find anything?
If you remove the typedef, it looks like this:


int IQ;
char* mpointer;

Yes, IQ and mpointer are like variable names (in the above code they are variable names).
The result is that after declaring a variable, you prefix it with a typedeff, and then the variable becomes an alias for the type.
That makes sense, right? Later, if you see that the typedef is very complex, you can remove the typedef to see what variable is declared, so that the alias represents such a variable.
 
Then, back to god's two complicated statements:
The same code at the page code block index 2

So let's look at the first one, which is very complicated, so let's get rid of the typedef and make it: void (Ref::*SEL_SCHEDULE)(float);
What this is, I can't really explain, I'm pretty watery in C++, but it's clearly a "function variable" (so to speak).
A function that returns void, takes a float, and qualifies its class as Ref.
 
Put the typedef back, this is the code that defines SEL_SCHEDULE as a function of the kind described above.
Then look at the second code, which is a macro:
1. Schedule_selector is the name of the macro
2._SELECTOR is an argument to a macro
3. Static_cast < Cocos2d: : SEL_SCHEDULE > (&_SELECTOR) is simplified to static_cast < SEL_SCHEDULE > (&_selector), this is a cast, which converts the _SELECTOR passed in to some type
4. What type do you want to convert? That's right, that's the type SEL_SCHEDULE, what type is SEL_SCHEDULE? As we've just explained, a function that returns a void, takes a float, and its class is Ref
 
Finally, let's look at how we normally use schedule_selector:


this->schedule(schedule_selector(HelloWorld::update));

The result is to convert our HelloWorld update function to the SEL_SCHEDULE type and pass it to the schedule function.
 
Ok, so now you see how our update function is defined: void update(float dt);
The return value is void, the argument is float, and the HelloWorld class inherits the Layer (and ultimately the Ref).
The type of the update function is basically the same as that of SEL_SCHEDULE, except for the type of the class.
But because the final base class for the Layer is Ref, you can cast the update function to the SEL_SCHEDULE type.
 
Ok, I explain to go on, more and more illogical, end here ~

4. New for loop (c++11)

C++11 adds a for loop syntax that makes it easy to iterate over groups, vectors, and arrays.
Simple to use, as follows:


    int nums[3] = { 1, 2, 3 };
    for (int num : nums) {
    }

This allows you to assign the value of nums to num, traversing the entire array without going through the ~

V. cctype standard library

I'm going to ask you A question, I'm going to give you the letter A, how do you determine whether it's uppercase or lowercase?
Xiao ruo: you said the letter A, of course it's A capital one! To judge?)
 
Ahem ~! Program! We are programmers, not so simple to judge ~! This is not rigorous ~!
Here's what we should do:


char c = 'A';
if(c >= 'A' and c <= 'Z') {
    //Well, it's capital
}

That's what most people think, right? (xiao ruo: no, most people know that A is capital, so don't judge.)

In fact, this is not safe, what if the character encoding is not ASCII? Maybe the answer is different.
So, we should write:


    char c = 'A';
    if (isupper(c)) {
        //Well, it's capital
        cout << "yes";
    }

Isupper is a function of the cctype library that determines if a letter is uppercase.
So it's more convenient and versatile,
 
Cctype library in the header file ctype.h (or cctype), more functions about this library, baidu, there will be a lot of ~

Sixth, the end

Ok, let's stop here this time. C++ is really not my favorite thing. There are few places where I can blow water. ~


Related articles: