本文共 940 字,大约阅读时间需要 3 分钟。
#ifndef _TEST_H#define _TEST_H#includeusing namespace std;char* func(){ char *ch = "hello world"; //can do like this because this is a static data member,it stored in a static data area,will not be released after the func //char ch[] = "hello world"; //can not do like this because ch[] is on stack, it will be released when the func finish return ch;}//it not safe because c is a local variable, it will be released when the func1 finish and will be used by others without any protection//but the return value record the adress of cint& func1(){ int a = 1; int b = 2; int c; c = a+b; cout << &c << endl; cout << c << endl; return c;}int main(){ int& result = func1(); cout << &result << endl; cout << result << endl; int &r = result; cout << &r << endl; cout << r << endl; //test the data type of a string when it represent in different ways /*char *result; result = func(); cout << result << endl; return 0;*/}#endif //_TEST_H
转载地址:http://vonmd.baihongyu.com/