constructor - Storage allocation of local variables inside a block in c++ -
i want know @ point compiler allocates storage local variables inside block. how goto , switch jump past constructor? :
class tree {/*...*/} ... void foo (int i){ if (i < 10) goto label; //illegal: cannot jump past ctor tree t (45); label: switch (i){ case 1: tree t2 (45); break; case 2: //illegal: cannot jump past ctor tree t3 (45); break; } }
while above code not work user-defined objects works if replace them built-in objects. why that?
edit: built in objects int, char, etc. errors (g++ 4.5 on ubuntu):
jumppastconstructor.c++: in function ‘void foo(int)’: jumppastconstructor.c++:26:3: error: jump label ‘label’ jumppastconstructor.c++:24:20: error: here jumppastconstructor.c++:25:10: error: crosses initialization of ‘tree t’ jumppastconstructor.c++:31:16: error: jump case label jumppastconstructor.c++:29:25: error: crosses initialization of ‘tree t2’
6.7/3:
it possible transfer block, not in way bypasses declarations initialization. program jumps point local variable automatic storage duration not in scope point in scope ill-formed unless variable has pod type (3.9) , declared without initializer (8.5).
what matters not when storage allocated, when constructor called. goto jumped past constructor problem, why it's banned. (pod types no initialiser don't need construction, they're allowed.)
Comments
Post a Comment