In: Computer Science
#include <iostream>
using namespace std;
string* callMe(string*& s){
static string* str = new string{"Chiqita Banana"};
str = s;
return str;
}
int main()
{
string* s = new string{"Hey Google"};
string* str = callMe(s);
cout << "Call me \'" + *str +"\'";
return 0;
}
1. What is the output or the error of this program?
Call me 'Hey Google'
2. And please explain the data flow in detail (to memory level)?
Please help question 2 with explanation, thank you!
IF YOU HAVE ANY QUERY PLEASE COMMENT BELOW.
Q1:
output is Call me 'Hey Google' as you mentioned.
Q2: Each statement is explained with comments.
#include <iostream>
using namespace std;
string* callMe(string*& s){ //function call me accept strifg oject and return string pointer// address
static string* str = new string{"Chiqita Banana"}; // static object of string is created which has same lifetime as lifetime of program. if this call me function is called more than once then this static object will use same space in memory. and will not allocate new space for str again.
str = s; //assigning address of s to str pointer.
return str; //returning address of str.
}
int main()
{
string* s = new string{"Hey Google"}; //this statement is creating string type object with value "Hey Google" in memory
string* str = callMe(s); //function call.
cout << "Call me \'" + *str +"\'";
return 0;
}
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP