• Code Reading


    0 why

    The purpose of this blog is to write down some useful tips in coding. 

    1 starting point of code:

    • c/c++: main
    • java: static void main
    • Microsoft windows: WinMain
    • Java applet or servlet hosts: init

    2 基本流程/操作中的tips

    this tip can make the if condition more concise

    • && / and: only evaluate the righthand side when the lefthand side is evaluated as "true".
    • || / false: only evaluate the righthand side when the lefthand side is evaluated as "false".
      • eg: fragment from ECHO
        1 if (*++argv && !strcmp(*argv, "-n")){
        2     ...
        3 }
        ECHO
    • assignment expression(赋值表达式) in a logic express
      • int c = 0;
      • while ((c = getopt (argc, argv, "t: "))  != -1)   // first execute assignment expression, and compare the lefthand variable with "-1"(a possible return value of getopt, which means the end of parsing in getopt.)

    3 static

     https://www.runoob.com/w3cnote/cpp-static-usage.html
    

    static 通常被认为同时具备全局和封装的特性。原因如下:

    • 在普通的面向过程编程中,如果需要定义一个可供下次调用的变量时(使得变量在函数外依然有效),不选择全局变量而选择static要更好。原因是static的作用范围可以限制在文件内
    #include <iostream>                                                                                                                                                                                                                    
    static int i;                                                                                                                                                                                                                                
    void Print(){                                                                                                                                                                                                                                
        std::cout << "The " << ++ i << "th times call of Print()
    ";                                                                                                                                                                             
    }                                                                                                                                                                                                                                             
    void Test() {                                                                                                                                                                                                                                
        for (int i=0; i < 10; ++ i) {
            Print();
        }
    }
    int main(){
        i = 0;
        Test();
        return 0;
    }
    static in progress oriented
    • 在面向过程的编程中,如果要在类对象之间共享一些数据,需要将变量定义为static类型
    // MyTest.h
    class MyTest {
    public:
        MyTest();
        ~MyTest();
    
        void Print();
    
    private:
        static int number_of_object;
    };
    
    
    // MyTest.cpp
    #include "../include/MyTest.h"
    #include <iostream>
    
    int MyTest::number_of_object = 0;
    MyTest::MyTest() {
        ++ number_of_object;
    }
    MyTest::~MyTest() {
    
    }
    void MyTest::Print() {
        std::cout << "number_of_object = " << number_of_object << std::endl;   
    }
    
    // main.cpp
    #include "./include/MyTest.h"
    
    void Test(){
        for (int i=0; i<10; ++ i) {
            MyTest t1;
            t1.Print();
        }    
    }
    int main() {
        Test();
        return 0;
    }
    static in class

    4 template class: 模板类

    
    
  • 相关阅读:
    java之 向上转型与向下转型
    java之 惰性初始化
    mysql 学习网站
    history of program atan2(y,x)和pow(x,y)
    atom 之 前端必备插件
    js之正则表达式
    Django之模型层
    每日作业5/28
    每日作业5/27
    每日作业5.26
  • 原文地址:https://www.cnblogs.com/ghjnwk/p/13366197.html
Copyright © 2020-2023  润新知