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 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
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
"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!
"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!
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
"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!
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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)...
|
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 =...
|
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
{
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |