2008 April 1st Tuesday (四月 一日 火曜日)

本文探讨了C++中非局部静态对象初始化的问题,并指出该问题在C++标准中并未明确规定。文章建议使用单例模式或其他替代方案来避免潜在问题,并提供了使用局部静态变量的一个示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chaos!!!  In C++ language, the question on when a non-local static object is initialized is not specified in C++ standard specification. So, if you abuse non-local static variables, it means that you find fault with yourself.   You can substitute Singleton pattern for non-local static variables, or adopt another way like following example. StaticObjct& getStaticObject(){   static StaticObject staticObj;     return staticObj; }   When meet a local static variable, C++ compilor initialize it at a time.  Maybe you can use "inline" modifier.  Don't forget the bad side of "inline".  It can duplicate variables more than one copy in your program; it just is recommand by a C++ compilor.   I had some misconstruction on C++, though I don't like it.  I am aware that C++ like perl.  They are all complex programming languages.  So, they can describe the complicated reality, they also perplex many people at the same time.   As a programmer, we should not run down C++ or other language else.  Everything has good sides has bad sides at once!  In my philosophy, everything is chaos!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值