473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Return by reference

Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;
};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous, eg if
I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.

Thanks
Mike
Jul 22 '05 #1
6 2195
Michael wrote:
When can i return by reference?
When the object a reference to which you're returning is still alive
and well after the function returns.
For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;
};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK?
Yes, perfectly OK.
I appreciate that in nots of cases this can be dangerous, eg if
I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.


Absolutely.

V
Jul 22 '05 #2
Michael posted:
Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;
};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous,
eg if I returned (Direction *4) as a tempory object would be created
with local scope then returning a reference to an invalid object.

Thanks
Mike

You can use a reference where you can use a pointer.

Your code in the above is valid (save for a typo or two).
There are also other things you can do with references:

int main()
{
std::string const &blah = std::string("Hello!");
//blah is now valid until the end of main
std::string const &poo = FuncThatReturnsStringByVALUE(); //Note: by
value
//poo is now valid until the end of main
}

The above is referred (no pun intended) to as "binding a temporary to a
reference".
-JKop
-JKop
Jul 22 '05 #3

"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@hercules.btinternet.com...
Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;
};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous, eg if I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.

Thanks
Mike


Sorry I didn't include all the mv3 operators but one of then allows me to
multiply by floats!
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL> wrote in message
news:iP*******************@news.indigo.ie...
Michael posted:
Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;
};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous,
eg if I returned (Direction *4) as a tempory object would be created
with local scope then returning a reference to an invalid object.

Thanks
Mike

You can use a reference where you can use a pointer.

Your code in the above is valid (save for a typo or two).
There are also other things you can do with references:

int main()
{
std::string const &blah = std::string("Hello!");
//blah is now valid until the end of main
std::string const &poo = FuncThatReturnsStringByVALUE(); //Note: by
value
//poo is now valid until the end of main
}

The above is referred (no pun intended) to as "binding a temporary to a
reference".
-JKop
-JKop


that is cool. Thanks!
Jul 22 '05 #5
Michael wrote:
Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;
};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous, eg if
I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.


Yes, but don't do it with regular functions by returning temporaries.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6
"Michael" <sl***********@hotmail.com> wrote in message news:ckk4t0
class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous, eg if I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.


It's fine and done often, as in vector<T>::operator[] which returns a T& or
const T&. Be aware of code like this and don't write it:

const mv3& f() {
MLine m;
return m.GetDirection();
}

int main() {
cout << f().x; // crash, memory access violation!
}
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wang Tong | last post by:
Suppose I have a function foo() and a class definition myClass, and I want to create an instance of myClass and return it from foo(). I guess the most efficient way is to return a reference to the...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
14
by: Gama Franco | last post by:
Hi, I'm designing an interface for a shared library, and I would like to know if there is a standard about how to return an object to the user. I will use exceptions to report errors, so there...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
12
by: huangshan | last post by:
hi all In what condition i need( or mast) a (templates)function return value by reference? can you give me a example thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.