[C++ Series Notes] 01 C++ and C

C++ and C

Hello world!

#include <iostream>
using namespace std;
int main() {
    
    
    cout << "Hello world!" << endl;
    system("pause");
    return EXIT_SUCCESS;
}

Namespace (scope)

  • Double colon ::scope operator{$namespace}::

    • If you do not write namespace, use the global namespace

      ::function(params);
      
  • Namespace: defined under the global namespace

    namespace {
          
          $namespace}{
          
          
    	// ...函数 类 结构体 变量...
    }
    
  • Namespaces can be nested

    namespace {
          
          $namespace}{
          
          
    	// ...函数 类 结构体 变量...
        namespace {
          
          $namespace}{
          
          
    	// ...函数 类 结构体 变量...
    	}
    }
    
  • Multiple identical namespaces will accumulate without overwriting

    namespace {
          
          $namespace}{
          
          
        int a;
        // ...
    }
    namespace {
          
          $namespace}{
          
          
        int b;
        // ...
    }
    // a b 均在 {$namespace} 中
    
  • The unnamed namespace is only available in the current file

    namespace {
          
          
        int a;
        // 相当于 static int a;
        // ...
    }
    
  • Namespace alias

    namespace anotherName = {
          
          $namespace};
    

C → C++ Similarities and Differences

  • Variable detection

    • c success cpp failure
    int a;
    int a = 1;
    // c success 
    // cpp failure
    
    • cpp success
    int a = 1;
    // cpp success
    
  • Function detection

    • c success cpp failure
    int function(p){
          
          // 形参类型
        // 返回值
    }
    // c success 
    // cpp failure
    
    • cpp success
    int function(int p){
          
          // 形参类型
        return 0;// 返回值
    }
    // cpp success
    
  • Function call detection

    • c success cpp failure
    int function(p){
          
          
        
    }
    int main(){
          
          
    	function(1, 2, 3);// 参数数量
    }
    // c success 
    // cpp failure
    
    • cpp success
    int function(int p){
          
          
        return 0;
    }
    int main(){
          
          
    	function(1, 2);// 参数数量
    }
    // cpp success
    
  • Type conversion

    • c success cpp failure
    char* p = malloc(4);// 隐式转换
    // c success 
    // cpp failure
    
    • cpp success
    char* p = (char*) malloc(4);// 隐式转换
    // cpp success
    
  • Structure

    • c failure cpp success
    struct Type{
          
          
    	void method();
    }
    // c failure 
    // cpp success
    
  • Ternary operator

    • c failure cpp success
    int a = 1, b = 2;
    (a > b ? a : b) = 3; 
    // C 报错,认为 = 左边不是一个左值
    // C 的三目返回一个值,而 C++ 返回引用
    
    • c success
    *(a > b ? &a : &b) = 3; 
    
  • const constant

    • C's const is a pseudo-constant, C++ is not

      const int a = 1;
      int* p = (int*)&a;
      *p = 2;
      // C 伪常量,可以修改,但在全局作用域声明的常量不可修改
      
      • c

        *p = 2 // p points to a

        a = 2

      • cpp

        *p = 2 // p points to a copy of a

        a = 1

      • Essentially, as long as the memory is allocated, it can be changed through pointers.

        And C++ accesses const variables (in most cases) through a hash table.

        Memory is allocated in custom data types or through variable initialization of const variables, which can be modified directly.

    • 'C considers const as an external variable by default, C++ does not

Quote

  • The function (expression) that returns the reference can be used as an lvalue (a usage that is easy to ignore)

  • The essence of reference is a pointer constant int* const var, and there is almost no difference between using pointers directly in the underlying implementation (assembly)

  • Reference to array

    int arr[10] = {
          
          ...};
    int (&arrRef)[10] = arr;
    // 注意 int& arrRef[10] 是引用的数组,即存放引用的数组
    
  • const int &ref = 10Constant reference (mainly used to modify formal parameters)

    const int &ref = 10;
    

    The underlying implementation is probably like this, memory is allocated and can be modified by pointers

    int temp = 10;
    int* const ref = temp;
    
  • C++11 rvalue references

    int&& a = 10;
    

    Must be an rvalue, but modification is allowed

Inline function

Used to replace C macro

  • Macro defects

    • readability

    • No type

    • Realization based on character replacement, no logic

      #define Max(a, n) ((a) < (b) ? (a) : (b)
      int main(){
              
              
          int a = 10;
      	Max(++a, 11);
          // 展开为 ((++a) < (11) ? (++a) : (11)
          // 很明显与预期不符
      }
      
  • Inline function

    inline void function(){
          
          
    	// 实现
    }
    

    Inline functions are actually code fragments, and calling inline functions is actually to run the code fragments directly.

    Save the overhead of function calls.

    • The following situations will cause the compiler not to think this is an inline function (even if you add the inline keyword)
      • There is a loop
      • Too many conditional judgments
      • The function body is huge
      • Address the inline function (inline function has no entry)
  • The member functions defined in the class are inline functions by default

Default parameters of the function

void function(int param1 = 1, string param2 = "2"){
    
    
	// 实现
}
int main(){
    
    
	function();
}
  • If the function declaration has default parameters, the function definition (implementation) must not

    That is, only one default parameter is allowed in the function declaration and definition

Placeholder parameters of the function

void function(int){
    
    
	// 实现
}

Mainly used to overload ++, as a placeholder parameter overload function.

Function overloading

  • Overloaded functions must be in the same scope

  • Conditions for distinguishing overloaded functions

    • Parameter list (type, number and order)
    • Note the ambiguity when there are default parameters
  • C linkability of C++ symbols

    C ++ calling C program compiled C of the situation can not link the function will appear.

    This is caused by the polymorphism of C++, which makes C++ have a different underlying implementation from C in terms of function names .

    Solution :

    • extern "C"Tell the compiler to compile the C++ function in C mode (that is, not to do the underlying polymorphism processing of C++) to ensure the C linkability of C++ calls.

      extern "C" void function();
      
    • Or directly include all in the C header fileextern "C" {}

      // 在 C 函数的头文件中
      #ifdef __cplusplus
      exteern "C"{
              
              
      #endif
      
      // 函数声明
      
      #ifdef __cplusplus
      }
      #endif
      

      There is also a problem:

      C++ directly calls C functions, VC++ compiles errors, g++ compiles no problem.

      Guess g++ secretly optimized.

Guess you like

Origin blog.csdn.net/qq_16181837/article/details/106555944