Local static object in C ++

zhaozj2021-02-12  151

A simple Class T

Class T {public: t () {value = 0x12345678;} ~ t () {value = 0;} int value;

Plus a simple foo function, define a static partial object:

Void foo () {static t t;}

After the compiler is expanded, it is actually the same as us.

Char Tmemory [SIZEOF (T)]; int Tinit = 0; void tfree () {((t *) tmemory -> ~ t ();}

Void foo () {if (! tinit) {Tinit = 1; New (tmemory) t (); atexit (tfree);}}

转载请注明原文地址:https://www.9cbs.com/read-6545.html

New Post(0)