Não é mais possível fazer postagens ou usar assinaturas novas da Usenet nos Grupos do Google. O conteúdo histórico continua disponível.
Dismiss

array as return value: possible?

0 visualização
Pular para a primeira mensagem não lida

Axel

não lida,
24 de out. de 2002, 10:35:1824/10/2002
para
I am trying to create a function wich deliver back an array,
like:

int DoSomething()
{
int val[7];
...
return val;
}

in this way it is not possible! Do I have to use pointers?
Has someone an idea?

thanks in advance
Axel


Neil Butterworth

não lida,
24 de out. de 2002, 10:43:5224/10/2002
para

"Axel" <a.w...@fh-oldenburg.de> wrote in message
news:ap90hb$sogbi$1...@ID-77641.news.dfncis.de...

> I am trying to create a function wich deliver back an array,
> like:
>
> int DoSomething()
> {
> int val[7];
> ...
> return val;
> }
>
> in this way it is not possible!

You cannot pass or return arrays by value in C++.

> Do I have to use pointers?

You could create an array dynamically and returna pointer to it, but I'd
suggest a better solution is to use a std::vector:

std::vector <int> DoSomething() {
std::vector <int> val(7);
...
return val;
}

NeilB


Axel

não lida,
24 de out. de 2002, 10:51:3124/10/2002
para
Great,
that' s the way it works.

cheers
Axel

"Neil Butterworth" <neil_but...@lineone.net> schrieb im Newsbeitrag
news:3db80...@mk-nntp-1.news.uk.worldonline.com...

Victor Bazarov

não lida,
24 de out. de 2002, 11:08:1224/10/2002
para
"Axel" <a.w...@fh-oldenburg.de> wrote...

> I am trying to create a function wich deliver back an array,
> like:
>
> int DoSomething()
> {
> int val[7];
> ...
> return val;
> }
>
> in this way it is not possible! Do I have to use pointers?
> Has someone an idea?


Arrays cannot be the return value type of functions. Use vector<>:

vector<int> DoSomething()
{


vector<int> val(7);
...
return val;
}

Victor
--
Please remove capital A's from my address when replying by mail


tom_usenet

não lida,
24 de out. de 2002, 12:08:0024/10/2002
para
On Thu, 24 Oct 2002 16:35:18 +0200, "Axel" <a.w...@fh-oldenburg.de>
wrote:

>I am trying to create a function wich deliver back an array,
>like:
>
>int DoSomething()
>{
> int val[7];
> ...
> return val;
>}
>
>in this way it is not possible! Do I have to use pointers?
>Has someone an idea?

In addition to the vector suggestion, you can return structs
containing arrays.

struct S
{
int val[7];
};

S DoSomething()
{
S s;
...
return s;
}

The array is copied when S is copied.

Tom

Alex Kro

não lida,
25 de out. de 2002, 04:03:3325/10/2002
para
"Victor Bazarov" <vAba...@dAnai.com> wrote in message
news:urg35br...@corp.supernews.com

> "Axel" <a.w...@fh-oldenburg.de> wrote...
>> I am trying to create a function wich deliver back an array,
>> like:
>>
>> int DoSomething()
>> {
>> int val[7];
>> ...
>> return val;
>> }
>>
>> in this way it is not possible! Do I have to use pointers?
>> Has someone an idea?
>
>
> Arrays cannot be the return value type of functions. Use vector<>:
>
> vector<int> DoSomething()
> {
> vector<int> val(7);
> ...
> return val;
> }

This is not very efficient, isn't it? A copy constructor is invoked to
return the vector by value. Although NRVO can be used here, it may not be
supported by your favourite compiler.

--
alex kro


Neil Butterworth

não lida,
25 de out. de 2002, 05:04:2525/10/2002
para
"Alex Kro" <res0...@verizon.net> wrote in message
news:pV6u9.9802$iV1....@nwrddc02.gnilink.net...

> "Victor Bazarov" <vAba...@dAnai.com> wrote in message
> news:urg35br...@corp.supernews.com
> > "Axel" <a.w...@fh-oldenburg.de> wrote...
> >> I am trying to create a function wich deliver back an array,
> >> like:
> >>
> >> int DoSomething()
> >> {
> >> int val[7];
> >> ...
> >> return val;
> >> }
> >>
> >> in this way it is not possible! Do I have to use pointers?
> >> Has someone an idea?
> >
> >
> > Arrays cannot be the return value type of functions. Use vector<>:
> >
> > vector<int> DoSomething()
> > {
> > vector<int> val(7);
> > ...
> > return val;
> > }
>
> This is not very efficient, isn't it?

It's very efficient from the standpoint of programmer time and program
correctness.

NeilB


Victor Bazarov

não lida,
25 de out. de 2002, 10:07:2325/10/2002
para
"Alex Kro" <res0...@verizon.net> wrote...


There are ways to overcome "inefficiency". First, on successful
proof of "inefficiency" not affecting overall program performance
(due to the fact that it happens very rarely, for example) the
whole thing can be left alone. Second, if there is a necessity
to avoid extra copying of the data, the MOJO technique could be
used (see latest posts from Andrei Alexandrescu in comp.std.c++).
You could also just wrap the array in an object that does efficient
memcpy or memmove (since the array is of fundamental type).

But before anything can happen there has to be proof of inefficiency.

E. Robert Tisdale

não lida,
25 de out. de 2002, 10:56:5025/10/2002
para
Alex Kro wrote:

> This is not very efficient, isn't it?

It is efficient.

> A copy constructor is invoked to return the vector by value.
> Although NRVO can be used here,

> it may not be supported by your favorite compiler.

If your favorite [C++] compiler does not support the NRVO,
then you should be shopping around for a C++ compiler
that does support the NRVO.

0 nova mensagem