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 }
assignment expression(赋值表达式) in a logic express
- eg: fragment from ECHO
- 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类型
// 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; }
4 template class: 模板类