[c++] Review coding skills for production by KKB ***
夯实基础,对齐概念
类的左值引用
打印类
IO 操作符通常对非公用数据成员进行读写,因此,类通常将 IO操作符设为友元。
friend ostream &operator<<(ostream &out, const Point &p)
命名空间
using std::endl using std::ostream;
四种范式
在本例子中依次得到体现:
1. Procedure-oriented
2. Object-oriented
3. Multi-paradigm programming
4. Functional Programming
#include <string> #include <map> #include <set> #include <vector> using namespace std; int add1(int a, int b) { return a + b; } class ADD { public: int operator()(int a, int b) { return a + b; } }; template<typename T, typename U> auto add3(T a, U b) -> decltype(a+b) { return a + b; } auto add4 = [](int a, int b) -> int { return a + b; }; int main() { ADD add2; cout << add1(3, 4) << endl; cout << add2(3, 4) << endl; cout << add3(3, 4) << endl; cout << add4(3, 4) << endl; return 0; }
编译与链接
nm -C other.o
例如,本文件有定义该函数,自然有一个地址。
Google测试框架
编译git clone的代码。
googletest/build$ make
Scanning dependencies of target gtest
[ 12%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 25%] Linking CXX static library ../lib/libgtest.a
[ 25%] Built target gtest
Scanning dependencies of target gmock
[ 37%] Building CXX object googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 50%] Linking CXX static library ../lib/libgmock.a
[ 50%] Built target gmock
Scanning dependencies of target gmock_main
[ 62%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 75%] Linking CXX static library ../lib/libgmock_main.a
[ 75%] Built target gmock_main
Scanning dependencies of target gtest_main
[ 87%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library ../lib/libgtest_main.a
[100%] Built target gtest_main
彩色打印:
int main() { printf("\033[0;1;33;41mhello world\033[0m\n"); printf("\033[0;1;32madd(3,4) = %d\n", add(3, 4)); return 0; }


浙公网安备 33010602011771号