Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to return by reference?

Reply
Thread Tools

How to return by reference?

 
 
Bo Yang
Guest
Posts: n/a
 
      11-29-2006
I know the below code is wrong:

Object & test(){ return Object() ;}
Object & v = test() ;

the Object is just a temporary variable,
so you the variable may be reference to
nothing.

And I try to this:

Object & test () { return * new Object(); }
Object & v = test () ;

But how can I destroy the Object's memery?

But, how to return reference safely?
Is there any principle to return by reference?
 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      11-29-2006
Bo Yang napsal:
> I know the below code is wrong:
>
> Object & test(){ return Object() ;}
> Object & v = test() ;
>
> the Object is just a temporary variable,
> so you the variable may be reference to
> nothing.
>
> And I try to this:
>
> Object & test () { return * new Object(); }
> Object & v = test () ;
>
> But how can I destroy the Object's memery?
>
> But, how to return reference safely?
> Is there any principle to return by reference?


By reference is usually returned
(1) *this (typically in overloaded operator=) or
(2) some class member instance or
(3) instance stored in some container

The main rule is "Instance must not be local variable of function,
which returns reference to it.

class Test
{
public:
Test(int data): data_(data) { }

// This example for case (1)
Test& operator=(const test& src)
{
data_ = src.data_;
return *this;
}

// This is example for case (2)
int& GetData() { return data_; }
};

 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      11-29-2006

Bo Yang wrote:
> I know the below code is wrong:
>
> Object & test(){ return Object() ;}
> Object & v = test() ;
>
> the Object is just a temporary variable,
> so you the variable may be reference to
> nothing.
>
> And I try to this:
>
> Object & test () { return * new Object(); }
> Object & v = test () ;
>
> But how can I destroy the Object's memery?


delete & v;

means delete the variable that v is a reference to which is the
unnamed Object created in test();


> But, how to return reference safely?
> Is there any principle to return by reference?


Usually you will also pass the object into the function by reference:

class Object{};
Object & test(Object &);


Object o;

Object & v = test(o);

v is 'another name' for o;

regards
Andy Little

 
Reply With Quote
 
Bo Yang
Guest
Posts: n/a
 
      11-29-2006
Ondra Holub :
> Bo Yang napsal:
>> I know the below code is wrong:
>>
>> Object & test(){ return Object() ;}
>> Object & v = test() ;
>>
>> the Object is just a temporary variable,
>> so you the variable may be reference to
>> nothing.
>>
>> And I try to this:
>>
>> Object & test () { return * new Object(); }
>> Object & v = test () ;
>>
>> But how can I destroy the Object's memery?
>>
>> But, how to return reference safely?
>> Is there any principle to return by reference?

>
> By reference is usually returned
> (1) *this (typically in overloaded operator=) or
> (2) some class member instance or
> (3) instance stored in some container
>
> The main rule is "Instance must not be local variable of function,
> which returns reference to it.


And when I implement a '-' operator in a class,
I must to do

Test & operator -( Test & right ) ;

How to implement this function then ?
I must to new a Test in this function , mustn't I ?
>
> class Test
> {
> public:
> Test(int data): data_(data) { }
>
> // This example for case (1)
> Test& operator=(const test& src)
> {
> data_ = src.data_;
> return *this;
> }
>
> // This is example for case (2)
> int& GetData() { return data_; }
> };
>

 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      11-29-2006

Bo Yang wrote:
> I know the below code is wrong:
>
> Object & test(){ return Object() ;}
> Object & v = test() ;
>
> the Object is just a temporary variable,
> so you the variable may be reference to
> nothing.
>
> And I try to this:
>
> Object & test () { return * new Object(); }
> Object & v = test () ;
>
> But how can I destroy the Object's memery?
>
> But, how to return reference safely?
> Is there any principle to return by reference?


you can return Object by value

Object test(){ return Object(); }

and then bind the return to reference on use

Object & obj = test();

If carefull, you can avoid copying

or, if Object is too big for stack, use

std::auto_ptr<Object> test(){ return std::auto_ptr<Object>(new Object);
}

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-29-2006
Bo Yang wrote:

>> By reference is usually returned
>> (1) *this (typically in overloaded operator=) or
>> (2) some class member instance or
>> (3) instance stored in some container
>>
>> The main rule is "Instance must not be local variable of function,
>> which returns reference to it.

>
> And when I implement a '-' operator in a class,
> I must to do
>
> Test & operator -( Test & right ) ;


Usually, you let that operator take a const reference as argument and return
an object instead of a reference.

> How to implement this function then ?
> I must to new a Test in this function , mustn't I ?


No. Well, you can, but shouldn't. You simply don't return by reference.

 
Reply With Quote
 
Bo Yang
Guest
Posts: n/a
 
      11-29-2006
Rolf Magnus :
> Bo Yang wrote:
>
>>> By reference is usually returned
>>> (1) *this (typically in overloaded operator=) or
>>> (2) some class member instance or
>>> (3) instance stored in some container
>>>
>>> The main rule is "Instance must not be local variable of function,
>>> which returns reference to it.

>> And when I implement a '-' operator in a class,
>> I must to do
>>
>> Test & operator -( Test & right ) ;

>
> Usually, you let that operator take a const reference as argument and return
> an object instead of a reference.


I have take a look at the FAQ and found
the return type is usually a value but a reference.
I understand the principle, return by reference only
if the object you return is all at your control.

Thank you!
>
>> How to implement this function then ?
>> I must to new a Test in this function , mustn't I ?

>
> No. Well, you can, but shouldn't. You simply don't return by reference.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
difference between return &*i and return i; Ganesh Gella C++ 4 11-12-2004 04:28 PM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57