×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

destructor being called when it shouldn't be...
3

destructor being called when it shouldn't be...

destructor being called when it shouldn't be...

(OP)
bitmap.h:

CODE

#ifndef MYBITMAP
#define MYBITMAP

class bitmap {

public:
    bitmap(void);
    bitmap(int,int);
    ~bitmap(void);

    static const int _default_width = 500;
    static const int _default_height = 500;

private:
    int _current_width;
    int _current_height;

};

#endif


bitmap.cpp:

CODE

#include "bitmap.h"
#include <stdio.h>
#include <iostream.h>

using namespace std;

bitmap::bitmap(void) {
    bitmap(_default_width,_default_height);
}

bitmap::bitmap(int setWidth, int setHeight) {
    _current_width = setWidth;
    _current_height = setHeight;
}

bitmap::~bitmap(void) {
    cout << "destructor called." << endl;
}


test-bitmap.cpp:

CODE

#include "bitmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>

using namespace std;

int main() {
    bitmap myBitmap;
    bitmap* myBitmap2 = new bitmap();

    cout << "should be before destructors." << endl;

    delete myBitmap2;

    return 0;
}


The output of the above code is intended to be:

CODE

should be before destructors.
destructor called.
destructor called.

However, what I get is this:

CODE

destructor called.
destructor called.
should be before destructors.
destructor called.
destructor called.

I am sure this is something simple and stupid I am missing.  Why are the destructors being called twice BEFORE the cout and then twice more after it?

RE: destructor being called when it shouldn't be...

Try removing the parentheses:

CODE

bitmap* myBitmap2 = new bitmap();

RE: destructor being called when it shouldn't be...

(OP)
i did remove the parenthesis, and no affect.

however, confusingly, if I change the declarations in test-bitmap.cpp to this:

CODE

bitmap myBitmap(30,40);
bitmap* myBitmap2 = new bitmap(45,55);

I get the expected result, that the destructors are only called at the end of the program, not during the declaration.

I *think* my problem is that I'm trying to call one constructor from another constructor.

RE: destructor being called when it shouldn't be...

Oops, I missed that...
Change:

CODE

bitmap::bitmap(void) {
    bitmap(_default_width,_default_height);
}
To:

CODE

bitmap::bitmap(void)
  :  bitmap(_default_width,_default_height)
{
}

RE: destructor being called when it shouldn't be...

Some addition: stop using deprecated <iostream.h> (use <iostream>) and f(void) (write f() for functions w/o parameters).
Call of bitmap(default_width,_default_height) in your snippet cteates local (in constructor) instantiation of bitmap object then discard it when bitmap() constructor body ended. It's an (meaningless in that case;) expression.

RE: destructor being called when it shouldn't be...

Why not just use default parameters

CODE

bitmap::bitmap(
   int setWidth = _default_width,
   int setHeight = _default_height)
{
    _current_width = setWidth;
    _current_height = setHeight;
}
Then you won't need the bitmap::bitmap () constructor.

RE: destructor being called when it shouldn't be...

(OP)
I am curious what is the difference (other than one constructor versus two) between the two methods shown, one by cpjust and one by xwb?

bitmap::bitmap(int setWidth = _default_width, int setHeight = _default_height) { ... }

-and-

bitmap::bitmap(void) : bitmap(_default_width,_default_height) { }

RE: destructor being called when it shouldn't be...

Not much, other than you only need to write 1 constructor instead of 2.  There might be a microscopic performance difference depending on how well your compiler's optimizer can do its magic.

RE: destructor being called when it shouldn't be...

(OP)
thanks, i think i have my resolution.  :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close