文章目录
1.派生类实现的父类虚函数后加上关键字 override 表示重载。
参考:c++11 - virtual? override? or both? C++ - Stack Overflow
如果重载失败,会在编译时报错,这样解决了自己以为重载了,但没有重载的问题。
2.class内的函数打出函数名和类名
代码如下:
#include <iostream>
#define star "*******"
#define intofunc " into function: "
#define intoclass " into class: "
#define exitfunc " exit function: "
#define printFunctionName std::cout << star << intofunc <<__func__<< star << std::endl;
#define printClassNameAndFunctionName std::cout << star <<intoclass <<typeid(this).name()<< intofunc <<__func__<< star << std::endl;
//#define printfunctionname ;
class A {
public:
void doSomeThing(void) {
printClassNameAndFunctionName
printFunctionName
//do some thing
}
};
int main() {
A().doSomeThing();
return 0;
}
3.C++多线程detach–后台线程
参考:c++ - When should I use std::thread::detach? - Stack Overflow
C++如果想创建后台运行的线程,需要在创建完成后进行detach
:
std::thread t([ ] {
printf("thread called with detach");
});
t.detach();
4.C++ lambda表达式
5.统计时间
参考:benchmarking - How to use clock() in C++ - Stack Overflow
#include <iostream>
#include <cstdio>
#include <ctime>
int main() {
std::clock_t start;
double duration;
start = std::clock();
/* Your algorithm here */
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout<<"printf: "<< duration <<'\n';
}