Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

array as return value: possible?

瀏覽次數:0 次
跳到第一則未讀訊息

Axel

未讀,
2002年10月24日 上午10:35:182002/10/24
收件者:
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

未讀,
2002年10月24日 上午10:43:522002/10/24
收件者:

"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

未讀,
2002年10月24日 上午10:51:312002/10/24
收件者:
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

未讀,
2002年10月24日 上午11:08:122002/10/24
收件者:
"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

未讀,
2002年10月24日 中午12:08:002002/10/24
收件者:
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

未讀,
2002年10月25日 凌晨4:03:332002/10/25
收件者:
"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

未讀,
2002年10月25日 清晨5:04:252002/10/25
收件者:
"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

未讀,
2002年10月25日 上午10:07:232002/10/25
收件者:
"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

未讀,
2002年10月25日 上午10:56:502002/10/25
收件者:
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 則新訊息