Pass temporary object to function that takes pointer
Posted
by Happy Mittal
on Stack Overflow
See other posts from Stack Overflow
or by Happy Mittal
Published on 2010-06-06T19:21:33Z
Indexed on
2010/06/06
19:32 UTC
Read the original article
Hit count: 243
I tried following code :
#include<iostream>
#include<string>
using namespace std;
string f1(string s)
{
return s="f1 called";
}
void f2(string *s)
{
cout<<*s<<endl;
}
int main()
{
string str;
f2(&f1(str));
}
But this code doesn't compile.
What I think is : f1 returns by value so it creates temporary, of which I am taking address and passing to f2.
Now Please explain me where I am thinking wrong?
© Stack Overflow or respective owner