|
![](/i/fill.gif) |
stbenge <^@hotmail.com> wrote:
> Warp wrote:
> > stbenge <THI### [at] hotmail com> wrote:
> >> hgeSprite *tile[8*8]
> >
> >> and filled it like this:
> >
> >> v=0;
> >> for(y=0;y<8;y++){
> >> for(x=0;x<8;x++){
> >> tile[int(v)]=new hgeSprite(tiles,x*132+1,y*132+1,128,128);
> >> v++;
> >> }
> >> }
> >
> > Does the HGE library force you to write code like that? If so, then it
> > really sucks.
> Don't use HGE to insult me...
I was not insulting you. I was asking if that's the only way of using
HGE (because if it is, then HGE really sucks as a C++ library).
> I could have
> written my loop a different way and used less code, but I feel
> comfortable typing it out that way.
The problem is not the loop. The problem is the 'news' and 'deletes'.
While there are situations where their use is completely ok and in fact
the best thing to do, those situations are usually rare.
Usually the "correct" way of, for example, creating a certain amount
of objects like that is:
std::vector<hgeSprite> tile;
tile.reserve(8*8);
for(...)
for(...)
tile.push_back(hgeSprite(...));
(The word "correct" is deliberately in quotation marks because it's,
quite naturally, not *always* the best way to do it. However, in most
cases this - or something similar - is.)
However, depending on how hgeSprite has been designed, that might not
be possible. For example, if it doesn't have a properly working copy
constructor (or it has been disabled), then that method cannot be used.
If that's the case, then you indeed have to either allocate things with
'new', or use other dirty tricks to get it working. And it would also be
a sign of sloppy design in the HGE library.
The problem with using 'new' and 'delete' like that is that it's very
error-prone. C++ has its quirks with memory allocation, and you just have
to learn to live with them if you want to use it. Encapsulation is the key.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |