|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi Everyone,
I have the following program, class A { int i; }; int main() { switch(1) { case 1 : // { A obj; printf("case 1\n"); // } break; case 2 : A obj1; printf("case 2\n"); break; } } and when i compile, i get an compilation error, saying, jump to case symbol enters scope of non-POD 'A obj' but if i remove the commented open and close brace, it works just fine, could anyone clarify the exact reason of the compiler reporting the error, Thanks in advance!!! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Rahul wrote:
> Hi Everyone, > > I have the following program, > > class A > { > int i; > }; > > int main() > { > switch(1) > { > case 1 : > // { > A obj; > printf("case 1\n"); > // } > break; > case 2 : > A obj1; > printf("case 2\n"); > break; > } > } > > > and when i compile, i get an compilation error, saying, > > jump to case symbol > enters scope of non-POD 'A obj' > > but if i remove the commented open and close brace, it works just > fine, could anyone clarify the exact reason of the compiler reporting > the error, These are the errors I get with gcc when I compile your code - I made a few changes i.e. made it a read non POD class. clcpp_t6.cpp: In function `int main()': clcpp_t6.cpp:20: error: jump to case label clcpp_t6.cpp:16: error: crosses initialization of `A obj' The errors should be self explanatory. #include <cstdio> class A { public: int i; A() : i() {} }; int main() { switch(1) { case 1 : // { A obj; std::printf("case 1\n"); // } break; case 2 : A obj1; std::printf("case 2\n"); break; } } |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 2008-02-06 04:15:14 -0500, Rahul <sam_cit@yahoo.co.in> said:
> > class A > { > int i; > }; > > int main() > { > switch(1) > { > case 1 : > // { > A obj; > printf("case 1\n"); > // } > break; > case 2 : > A obj1; > printf("case 2\n"); > break; > } > } This is illegal. When should obj's destructor be run? -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Feb 6, 2:07 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-02-06 04:15:14 -0500, Rahul <sam_...@yahoo.co.in> said: > > class A > > { > > int i; > > }; > > int main() > > { > > switch(1) > > { > > case 1 : > > // { > > A obj; > > printf("case 1\n"); > > // } > > break; > > case 2 : > > A obj1; > > printf("case 2\n"); > > break; > > } > > } > This is illegal. When should obj's destructor be run? When you leave scope. There's no problem with the destructor (formally, at least); you're allowed to jump out of scope, and it's up to the compiler to call any destructors. (And of course, the semantics of switch/break are those of goto.) The problem is rather when will the initialization occur. In the above example, without the {}, obj is in scope, visible and usable in case 2. The code jumps over an initialization. (And I don't think that this exact example is illegal---class A doesn't have any initialization. But it would be illegal if class A had a non-trivial constructor, or if obj had any initialization expression.) The reason why the declaration of obj1 is legal, of course, is that there isn't any label after it. It is the label ("case 2:") after the declaration which causes the code to be illegal. Of course, the reason why you're not allowed to jump over non-trivial initialization is because doing anything with the object afterwards is very problematic. And in C++, you will do something with the object when it goes out of scope: call the destructor. (Presumably, if the object has trivial initialization, the destructor knows how to cope with it, and calling the destructor without having done the initialization is OK. In practice, I don't think I've ever seen an object with a non-trivial destructor which didn't have non-trivial constructors as well.) -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 2008-02-07 04:46:30 -0500, James Kanze <james.kanze@gmail.com> said:
> >> This is illegal. When should obj's destructor be run? > > When you leave scope. There's no problem with the destructor > (formally, at least); On the contrary: in some cases, the destructor should be run, and in others it should not. Please note that some responses are not intended to convey the exact formal details of the C++ standard, but to give a sense of the reasons behind the rules. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 6 Feb, 14:07, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-02-06 04:15:14 -0500, Rahul <sam_...@yahoo.co.in> said: > > int main() > > { > > switch(1) > > { > > case 1 : > > // { > > A obj; > > printf("case 1\n"); > > // } > > break; > > case 2 : > > A obj1; > > printf("case 2\n"); > > break; > > } > > } > > This is illegal. When should obj's destructor be run? > I fail to see how this is illegal, even if the brackets are not there: A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed __unless__ the variable has POD-type and is declared without an initializer. (6.7/3, emphasis mine) > > jump to case symbol > > enters scope of non-POD 'A obj' If A turned out to be non-POD it would be illegal and the compiler error would be correct. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Feb 8, 8:10 am, Triple-DES <fire13...@hotmail.com> wrote:
> On 6 Feb, 14:07, Pete Becker <p...@versatilecoding.com> wrote: > > On 2008-02-06 04:15:14 -0500, Rahul <sam_...@yahoo.co.in> said: > > > int main() > > > { > > > switch(1) > > > { > > > case 1 : > > > // { > > > A obj; > > > printf("case 1\n"); > > > // } > > > break; > > > case 2 : > > > A obj1; > > > printf("case 2\n"); > > > break; > > > } > > > } > > This is illegal. When should obj's destructor be run? > I fail to see how this is illegal, even if the brackets are not there: > A program that jumps from a point where a local variable with > automatic storage duration is not in scope to a point where it is in > scope is ill-formed __unless__ the variable has POD-type and is > declared without an initializer. (6.7/3, emphasis mine) > > > jump to case symbol > > > enters scope of non-POD 'A obj' > If A turned out to be non-POD it would be illegal and the compiler > error would be correct. In the original code, A was a non POD type (it has a private data member), which is why it was illegal. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
![]() |
| Outils de la discussion | |
|
|