How to return by reference?

B

Bo Yang

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?
 
O

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.

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_; }
};
 
K

kwikius

Bo said:
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
 
B

Bo Yang

Ondra Holub :
Bo Yang napsal:

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 ?
 
D

dasjotre

Bo said:
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);
}
 
R

Rolf Magnus

Bo said:
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.
 
B

Bo Yang

Rolf Magnus :
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top