Object gets destructed before constructed

Object gets destructed before constructed

Post by Kok Ch » Sun, 07 Jul 1996 04:00:00



Is there a way to blank multiple screens/monitors using Powerplant classes
 or is there any ways to do it? Any advise is greatly appreicated.

--chan

 
 
 

1. Object gets destructed before constructed

The following behavior doesn't quite seem right.  If an object is created
on the stack in one of the cases of a switch statement, the object's
destructor gets implicitly called when the switch block ends.  However, if
we skip over the case that declares the object, the object's destructor
still gets implicitly called when the switch block ends.  This causes
problems- specifically due to a destructors probable dependence on data
that it expected the constructor to initialize.  And the constructor only
gets called in the case statement it was declared in.

I understand that all cases in a switch share the same local block, and I
know that my problem could be easily solved by putting a couple curly
brackets in,  but it seems to me that this is still a problem (I don't
have any of my C++ books, so I can't even try to look it up)

Included is sample source/execution that demonstrates problem.

Execution #1: GOOD CASE
Enter a number 1 or 2: 1
case 1
duh constructor called
duh destructor called

Execution #2: PROBLEM CASE
Enter a number 1 or 2: 2
case 2
duh destructor called

SOURCE:
#include <iostream.h>

class Duh {
public:
   Duh() { cerr << "duh constructor called\n"; }
   ~Duh() { cerr << "duh destructor called\n"; }

int main()
{
   int x;

   cout << "Enter a number 1 or 2: ";
   cin >> x;

   switch (x) {
      case 1:
         cerr << "case 1\n";
         Duh duh;
         break;
      case 2:
         cerr << "case 2\n";
         break;
   }

--

http://sdcc8.ucsd.edu/~jantonel
UC San Diego, Computer Engineering

2. Periodic Post

3. CW11 static objects not destructed

4. truefonts in emacs/xemacs

5. Problem constructing Java ArrayList Object

6. y'a t'il des francais sur ce news groupe

7. When are object-variables constructed?

8. Trashing Locked Files

9. Global objects constructed/destroyed twice??

10. When do global objects get constructed in a code resource?

11. PowerPlant and TheDebugger ... Getting the Object Tables to

12. Help needed... Getting pointer to object in form

13. caught exception destructed twice