[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: passing temporary storage as a non-const parameter to a function
From: |
Ulrich Eckhardt |
Subject: |
Re: passing temporary storage as a non-const parameter to a function |
Date: |
Tue, 09 Nov 2004 22:06:00 +0100 |
User-agent: |
KNode/0.7.7 |
jjleto wrote:
> void f1(const string& msg) { cout << msg << endl; }
> void f2(string& msg) { cout << msg << endl; }
>
> int main()
> {
> f1(string("foo")); // OK
> f2(string("foo")); // NOK.
> }
>
> I've got the following error on the line calling f2():
>
> error: invalid initialization of non-const reference of type
> 'std::string&' from a temporary of type 'std::string' (gcc 3.3.4)
Nothing to do with g++, just normal C++. Other than the solutions already
proposed, you can also const_cast the problem away.
Uli
--
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/