|
 |
Warp wrote:
> stbenge <^@hotmail.com> wrote:
>> 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).
Then please accept my apology :)
>> 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.
In the HGE tutorials, sprites are created in this manner, so I adapted
the method to work with arrays.
> 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.
Thank you very much for this tip. I seem to remember that hgeSprite does
have a functioning copy constructor, so I should give the vector method
a go. Before I get there, I have to gain a better understanding of OOP
programming so I don't find myself hacking and experimenting with the
std::vector functions. Too much of my time is spent doing this, I'm
ashamed to admit :(
> 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.
Another reason to get myself familiarized with all that C++ has to
offer. Thank you for your feedback!
Sam
Post a reply to this message
|
 |