[comp.lang.c++] constructors

mgardi@watdcsu.waterloo.edu (M.Gardi - ICR) (11/26/87)

being brand new to C++ programming, I have a few questions....it seems that
'reading' about c++ is fine...actually programming is another matter!
 
I currently have a structure consisting of the following:
struct bitmap {
    int length;
    int width;
    unsigned char *bitshp;
    }
 
I would like to implement this as a class with an appropriate constructor.
Would I normally declare a bitmap pointer in my declarations and then in my
program simply write   'variable = new bitmap(length,width)' ?
 
The real question I am wondering about is normally, I would allocate store for 
the bitshp using malloc(width * 8 * length).
In my constructor, do I now use 'bitshp = new char[length * width * 8]' ?
 
also, I guess my desctructor would now have to 'delete bitshp' and        
'delete bitmap' ?
 
thanks for any help 
 
 
peter devries

grunwald@uiucdcsm.cs.uiuc.edu (11/29/87)

you need to say what you mean, not what you're used to saying.

i.e. don't look at it as building a struct. look at it as a class:

class Bitmap {
	int width;
	int height;
	char *theMap;
public:
	Bitmap(int xwidth, int xheight) {
		width = xwidth; height = xheight;
		int bytes = ((width + 7) / 8) * height;
		theMap = new char[byte];
	}

	~Bitmap() {
		delete theMap;
	}
}

this way, when you 'delete' a Bitmap, you also delete the store associated
with it (because its destructor is called).

you could also declare member functions like 'rasterOp' and the like.