c++函数返回值

it2025-04-08  11

const string getStr()

{

return "aaaa";

}

char &get_val(string &str, string::size_type ix)

{

return str[ix];

}

int odd[]{1,2,3,4,5};

int even[]{6,7,8,9,0};

 

//decltype:并不负责把数组类型转换成对应的指针,所以decltype的结果是个数组,想要函数返回指针必须加一个*

decltype(odd) *arrp(int i)

{

return (i % 2) ? &odd : &even;

}

int main()

{

//函数返回,返回的值和初始化变量一样

string str = getStr();

str += "bbb";

cout << str << endl;

 

//调用一个返回引用的函数得到左值,其他返回得到右值

string s("abcdefg");

cout << s << endl;

get_val(s, 0) = 'A';

cout << s << endl;

 

//返回数组指针,因为数组不能拷贝,所以函数返回的是数组的指针或引用

//使用decltype

//int (*oodp)[5] = arrp(1);

 

return 0;

}

最新回复(0)