A
Andriy Shnyr
Hallo!
I have the following example:
class Test{
public:
Test(int const level): _level(level), _next(0){}
void add_next(Test* const next){
_next = next;
}
Test* const& get(int const level){
if(_next != 0 && _next->_level >= level)
return _next->get(level);
else
return this;
}
void print(){
cout << _level<< endl;
}
private:
Test* _next;
int const _level;
};
Compiling this example with gcc 3.2 results in the following warning
"Returning reference to temporary"
line "return this"
Ok, I understand may be it is enough to return "T* const" not a
reference, but still I do not understand the reason:-(
Is is safe to return such a reference?
I have the following example:
class Test{
public:
Test(int const level): _level(level), _next(0){}
void add_next(Test* const next){
_next = next;
}
Test* const& get(int const level){
if(_next != 0 && _next->_level >= level)
return _next->get(level);
else
return this;
}
void print(){
cout << _level<< endl;
}
private:
Test* _next;
int const _level;
};
Compiling this example with gcc 3.2 results in the following warning
"Returning reference to temporary"
line "return this"
Ok, I understand may be it is enough to return "T* const" not a
reference, but still I do not understand the reason:-(
Is is safe to return such a reference?