利用变量的生命周期实现在main入口前执行一段程序

版权声明:本文为博主原创文章,如有需要,可以转载,但请注明出处。 https://blog.csdn.net/xunye_dream/article/details/84921610

直接上代码了。

#include <iostream>
 
using namespace std;
 
class ShowLog
{
public:
	ShowLog()
	{
		cout << __func__ << ": Hello Kity" << endl;
		print();
	}
 
	void print() const
	{
		cout << __func__ << ": I just doing something. HAHA" << endl;
	}
 
	~ShowLog()
	{
		cout << __func__ << ": Goodbye Kity" << endl;
	}
};
 
static ShowLog showLog;
 
int main(int argc, char **argv)
{
	cout << __func__ << ": Bingo, Good Idea" << endl;
	return 0;
}

输出结果:

ShowLog: Hello Kity
print: I just doing something. HAHA
main: Bingo, Good Idea
~ShowLog: Goodbye Kity

猜你喜欢

转载自blog.csdn.net/xunye_dream/article/details/84921610