Re: Allocating Memory... which is best practice?

  • Follow


A completely different alternative from the suggestions you received so
far is to pass an output iterator to your function so that its users
are free to choose the container in which they want your characters.
Something like

template <typename OutIter> void f(OutIter oi)
{
  while (thereAreMoreChars())
    *oi++ = nextChar();
}

Cheers,
Nicola Musatti


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply nicola.musatti423 (66) 3/22/2005 11:12:06 PM

Nicola Musatti wrote:
> A completely different alternative from the suggestions you received so
> far is to pass an output iterator to your function so that its users
> are free to choose the container in which they want your characters.
> Something like
>
> template <typename OutIter> void f(OutIter oi)
> {
>   while (thereAreMoreChars())
>     *oi++ = nextChar();
> }


Wow, that's nice.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Jeff 3/23/2005 5:27:55 PM


Hi!

Jeff Schwab wrote:
> Wow, that's nice.

That's the C++-way I'd prefer. It avoids any memory allocation 
issues present in previous examples.

Frank


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Frank 3/23/2005 10:55:02 PM

2 Replies
170 Views

(page loaded in 0.078 seconds)


Reply: