实现.cpp
int test(int a, int b)
{
cout <<"normal a:" <<a << " " <<"normal b:"<< b << endl;
int c = a;
cout << "c=a , c:" <<c<< endl;
return c;
}
int test2(int &a, int &b)
{
cout <<"&a:"<< a << " " <<" &b:"<<b << endl;
int c = a;
cout << "c:" << c<<endl;
return c;
}
int test3(int *a, int *b)
{
cout <<"*a:"<< a << " " <<"*b:"<< b << endl;
int c = *a;
cout << "c:" <<c<< endl;
return c;
}
int test4(int a, int b)
{
cout <<"normal a:"<< a << " " <<"normal b:"<< b << endl;
int *p = &a;
cout << "*c=&a , c:" << *p << endl;
return *p;
}
int *test5(int a, int b)
{
int *c = &a;
return c;
}
/*error
1.错误 1 error C2440: “return”: 无法从“int”转换为“int *”
2. IntelliSense: 返回值类型与函数类型不匹配
int *test10(int a, int b)
{
int *c = &a;
return *c;
}*/
int &test6(int a, int b)
{
int *c = &a;
return *c;
}
int &test7(int a)
{
return a;
}
int test8(int a)
{
cout << "a:" << a << " " << "&local a:" << &a << endl;
a += 1;
cout << "local a+1:" << a << endl;
return a;
}
class Ctest
{
int m_a;
int b;
public:
Ctest(int a,int b):m_a(a),b(b){}
friend ostream &operator<< (ostream &out, const Ctest &ts)
{
out << ts.m_a << ts.b << endl;
return out;
}
int t(){ return m_a; }
};
main.cpp
int main()
{
int a = 1, b = 2;
int e, f;
test(a, b);
int d = test(a, b);
test2(a, b);
test3(&a, &b);
test4(a, b);
test5(a, b);
Ctest ts(1,2);
Point p;
cout << p.GetX() << endl;
int c = ts.t();
cout << "========================" << endl;
cout << test(a, b) << "--" << test2(a, b) << "--" << test3(&a, &b) << "--" << ts.t()
<< "--" << test4(a, b) << "--" << "*test5 return c" << test5(a, b) << "==" << "&test6 return local*c:"
<<test6(a,b)<<"=="<<"&test7,return a:"<<test7(a)<<endl;
cout << "***************************" << endl;
cout << "&a in main:" << &a << " " << "*test8(a):" << (void*)test8(a) << " " << "test8(a)" << test8(a)<< endl;
system("PAUSE");
return 0;
}