Re: How to return an array of own datatype
From: Dietmar Kuehl (dietmar_kuehl_at_yahoo.com)
Date: 01/03/05
- Next message: Ioannis Vranos: "Re: Help, installing MinGW & Dev-Cpp"
- Previous message: Ivan Vecerina: "Re: If I change from std::vector to std::list my compiler becomes very displeased with me"
- In reply to: Steve: "How to return an array of own datatype"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 3 Jan 2005 04:53:30 -0800
Steve wrote:
> String[] String::tokenize(const String s, char* delimiter)
> {
> String stringArray[];
> // tokenize the String and put each word into the String array
> ...
>
> return stringArray;
> }
The typical C++ approach to this is to have the function take an output
iterator which is written to in the function, i.e. something like this:
| template <typename OutIt>
| OutIt String::tokenize(char const* delimiters, OutIt to)
| {
| // ...
| *to++ = token;
| // ...
| return to;
| }
You could then use your 'tokenize()' function something like this:
| std::vector<String> vec;
| String str(/*...*/);
| str.tokenize(",", std::back_inserter(vec));
... or use whatever appropriate container type you wish instead of
'std::vector' as long as it supports a 'push_back()' operation. This
also
allows output e.g. to standard output using an appropriate iteartor:
| str.tokenize(',', std::ostream_iterator<String>(std::cout, "\n"));
The trouble is somewhat that your teacher apparently does not
understand
how C++ works and deprived you from reasonable use of the standard
library
thereby either forcing you to reimplement fairly large portions thereof
or
using inferior programming techniques. If he wanted you to provide an
implementation of some particular algorithms without using the
corresponding
algorithms from the standard library he should have specified it that
way
rather.
> String[] tokenize(const String s, char* delimiters);
This is illegal syntax. If it were possible to return an array from a
function, the syntax would rather be something like this:
| String (tokenize(String const&s, char const* delimiters))[10];
A declaration like this should give an error message similar to the one
issued e.g. by SUN's compiler:
| Error: Functions cannot return arrays or functions.
However, please note two other important changes I applied to the
function
signature:
- The string is taken as reference argument: there is probably no need
to
copy the String, especially if you want it to be 'const' anyway.
- To allow passing of string literals to the functions, the second
parameter uses a pointer to 'char const' (i.e. an array of constant
'char's; 'char const' and 'const char' is equivalent but I consider
it
to be easier to read to place the 'const' as far to the right as
possible).
> My first problem is that I get a bunch of errors for the header line
> (the first is C3409, I'm using VC++ .NET),
> I assume this is not the way how one should define an array return
type?
Right. See above.
> I know I have a second problem too, because I have to define the size
> of the array I want to return, but honestly I want to avoid that, I
> would love to do that dynamicly
> as in a Vector.
To do so, you need to handle dynamic memory allocation - or,
preferably,
use one of the standard containers. Proper implementation of a
container
is actually non-trivial and involves loads of intrinsic little nits
which
are, IMO, far beyond a typical school assignment level. For example,
reasonable implementation of a 'std::vector' replacement involves
allocation
of uninitialized memory and placement construction/destruction of
elements.
No real rocket science but hardly what students need to be exposed to
learn
dealing with data structures either. Of course, some issues can be side
stepped by default constructing a bigger array and only considering a
subset
of the actual elements to be present in the vector...
-- <mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/> <http://www.contendix.com> - Software Development & Consulting
- Next message: Ioannis Vranos: "Re: Help, installing MinGW & Dev-Cpp"
- Previous message: Ivan Vecerina: "Re: If I change from std::vector to std::list my compiler becomes very displeased with me"
- In reply to: Steve: "How to return an array of own datatype"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|