Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

shadedecho

Programmer
Oct 4, 2002
336
US
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?
 
Try removing the parentheses:
Code:
bitmap* myBitmap2 = new bitmap[b][COLOR=red]()[/color][/b];
 
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.
 
Oops, I missed that...
Change:
Code:
bitmap::bitmap(void) {
    bitmap(_default_width,_default_height);
}
To:
Code:
bitmap::bitmap(void)
  :  bitmap(_default_width,_default_height)
{
}
 
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.
 
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.
 
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) { }
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top