POV-Ray : Newsgroups : povray.off-topic : New NearInfinity demo up : Re: New NearInfinity demo up Server Time
6 Sep 2024 15:20:33 EDT (-0400)
  Re: New NearInfinity demo up  
From: Warp
Date: 13 Dec 2008 16:43:17
Message: <49442c75@news.povray.org>
stbenge <^@hotmail.com> wrote:
> Warp wrote:
> > stbenge <THI### [at] hotmailcom> 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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.