C++ way for C thing

Started by
3 comments, last by Spadge 21 years, 3 months ago
Hiya all, I''m currently reading through Tricks of the Windows Game Programming Gurus and got to the part where the author gives you a function for creating a Direct Draw clipper and getting it working. I have a question about this (which I think looks ugly):
LPRGNDATA region_data;
region_data = (LPRGNDATA)malloc(sizeof(RGNDATAHEADER)+num_rects*sizeof(RECT)); 
What is the C++ way to do this?Because for a starters I''d like to know, and secondly I''m going to be re-writing all his functions he gives you for doing DirectDraw into classes and such. I''ve consulted my C++ book but it only gives at most
new int 
, so on and so forth. Thanks in advance! Oh, and Happy Christmas! (Unless you don''t celebrate in which case; Happy December! Heh) ------- Spadge.
-------Spadge.
Advertisement

  region_data = new LPRGNDATA[sizeof(RECT)*num_rects];//can''t guarantee accuracy though....  


Merry Christmas

Beginner in Game Development?  Read here. And read here.

 

LPRGNDATA region_data = (LPRGNDATA) new char [sizeof(RGNDATAHEADER)+num_rects*sizeof(RECT))];


What does God want?
Does God want goodness or the choice of goodness?
Is a man who chooses the bad perhaps in some way better than a man who has the good imposed upon him?
Domine non secundum peccata nostra facias nobis
oh!
sorry....

question is there you picked char instead of DWORD, int or something else?

(note: it''s just as ugly as the C version lol)

Beginner in Game Development?  Read here. And read here.

 

The C++ way would be to write a constructor that takes the number of rects as a parameter. Then, you''d do this:

MyRegionData* pointer_to_region_data = new MyRegionData(num_rects); 


Alternatively, you could decide to create the object on the stack.
MyRegionData region_data = MyRegionData(num_rects); 

This topic is closed to new replies.

Advertisement