BobR wrote in message...
> Juha Nieminen <nos@thanks.invalid> wrote in message...
> > Ron Natalie wrote:
> > > No. The return is a temporary rvalue. If you bind it to a const
> > > reference it will live as long as the reference.
> > Is this also true for const references as member variables? If I try
> > this, it compiles without warnings but causes a segmentation fault
> > (at least with gcc 3.3):
> > #include <iostream>
> > #include <string>
> > class A{
> > const std::string& s;
> > public:
> > A(): s("test2") {}
> > void print() { std::cout << s << std::endl; }
> > };
> > int main(){
> > A a;
> > a.print();
> > }
> Think about what you are initializing the reference to.
> std::string &rs( "Hi There" );
> // Error: could not convert `"Hi There"' to `std::string&'
> class A{
> std::string s;
> const std::string &rs;
> public:
> A(): s( "test2" ), rs( s ){}
> void print() { std::cout << rs << std::endl;}
> };
> int main(){
> A a;
> a.print();
> }
Oooops, sorry Juha. I see what you were doing now. Oh well, maybe my example will help some newbie.
--
Bob R
POVrookie