How to learn c + +

Scene at that time

Dialogue:

Narrator: work for 3-5 years and go for an interview c++

Interviewer: do you know more about c + +?

Xiao Wang: Yes

Narrator: full of confidence

Interviewer: let's take a few c + + questions

  • Printout results?
#include <stdio.h>
#define MIN__TEST(a,b) ((a)<=(b)?(a):(b))
int main()
{
    int arry[5] = {10,20,30,40,50};
    int *p = &arry[0];
    //printf("1=%d\n",*p);
   //printf("2=%d\n",*p++);

    //printf("MIN = %d, *p = %d\n", MIN__TEST(*p++, 10), *p);
    return 0;

}
  • Printout results:
float a = 1.0f;
std::cout << (int)a << std::endl; //1
std::cout << (int&)a << std::endl;//????/
std::cout << std::boolalpha << ( (int)a == (int&)a ) << std::endl;

Xiao Wang:...

Narrator: inner collapse

Knowledge point 1: #define macro's advantages and disadvantages

Macros are not functions, macros are not statements, and macros are not type definitions

Macros are not functions, macros are not statements, and macros are not type definitions

Macros are not functions, macros are not statements, and macros are not type definitions

Dialogue:

Lao Wang suggested: look at the difference between macro and const and introverted functions?

Xiao Wang asked: macro and const are different things. How can they be different? Have questions about the topic?

Narrator: a vast ocean, where is it

Lao Wang:

  • Macros can modify constants const modifies the same points as constants. #define Pi 3.1415926 const double =3.1415926; (during compilation, the read-only operation const is used for replacement)
  • Macro instead of function, introverted function is also the same point of function. What about the same point?

#define MIN__TEST(a,b) ((a)<=(b)?(a):(b))

Tip: what is the difference between variables and expressions?

Xiao Wang:

  • Preprocessing commands: do not compile directly, do not perform type checking, and do some replacement work that does not conform to c + + syntax (advantages)
  • Improve program efficiency (advantages, avoid function calls)
  • Macros do not check for errors, const checks for errors (disadvantages)

Macro: judge the error scenario of two numbers (disadvantage: expression as parameter)

A cycle i=1 becomes i=3. It does not meet the expectation

Observation:

  • The above is a function that iterates through the loop, and then compares the size through the macro max
  • i + +, written in the macro, causes i to do + + twice, from 1 to 3. (Narrator: wrong) bigest =max(2>2?2:3)
  • How to write the correct macro

Answer: 20 30 think about why

#include <stdio.h>
#define MIN__TEST(a,b) ((a)<=(b)?(a):(b))
int main()
{
    int arry[5] = {10,20,30,40,50};
    int *p = &arry[0];
    //printf("1=%d\n",*p);
   //printf("2=%d\n",*p++);
   
    //printf("MIN = %d, *p = %d\n", MIN__TEST(*p++, 10), *p);
    return 0;
    
}
wan

Test point 2: virtual memory address and pointer type

Pointer is the soul of C language. To learn pointers well, we must first learn from the memory address.

The memory address is an identification of the data stored in the memory, not the data itself. The data stored in the memory can be found through the memory address

  • Xiao Wang asked:

Memory address is a string of hexadecimal numbers (in fact, the bottom layer is binary). Where does it come from? What type? Why do you need a type?

Try to answer:

#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{ 

float a = 1.0f;
std::cout << (int)a << std::endl; //1
std::cout << (int&)a << std::endl;//1065353216
std::cout << std::boolalpha << ( (int)a == (int&)a ) << std::endl;  //false

float b = 0.0f;
std::cout << (int)b << std::endl; //0
std::cout << (int&)b << std::endl; //0
std::cout << std::boolalpha << ( (int)b == (int& )b ) << std::endl;//true 
   
return 0;

}
   

1 address

0 address

Library: linking and loading.

Explore the compilation order of C + + class member functions

  • C language function compilation order: compile from top to bottom. When other function names used inside the function are encountered, they will be searched before the function. If there is a function declaration or definition, the compilation will succeed, otherwise the compilation will fail.
  • Compilation order of member functions in C + + classes: 1. First compile the declaration of members. 2. Do not compile the function body until all classes are visible.

C language function compilation order

Compilation order of member functions in C + + classes:

One time completion

2 times

Global and local symbols

The staic modifier is local

  1. Xiao Wang asked: what is the difference between static local variables and ordinary global variables and static global variables? https://www.zhihu.com/question/37029880
  • There is a symbol table in the C + + relocatable target module (i.e. the. o file), which contains all the symbol information defined and referenced by this module. Symbols are divided into global symbols and local symbols.
  • Global symbols refer to non static functions and global variables defined in this module. They are visible to other modules and can be used by other modules.
  • Local symbols refer to static functions and static variables, which can only be used by this module and are not visible to other modules. Use the readelf command to see whether a symbol is local or global.

All are stored in data segments, and global variables, static local variables and static global variables are allocated space in the static storage area

But the scope is different.

  1. What about symbol conflicts?

What about the conflict?

Posted on Thu, 18 Nov 2021 01:56:33 -0500 by beinerts