C++传入参数和返回参数

实现.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;
}
//*test5 return c
int *test5(int a, int b)
{
	//cout << "*test5, return local c" << endl;
	int *c = &a;
	return c;
}

/*error
1.错误	1	error C2440:return: 无法从“int”转换为“int *2.	IntelliSense:  返回值类型与函数类型不匹配	

int *test10(int a, int b)
{
	//cout << "*test5, return local c" << endl;
	int *c = &a;
	return *c;
}*/

//&test6 return local *c
int &test6(int a, int b)
{

	int *c = &a;
	return *c;
}

//&test7 return a
int &test7(int a)
{
	return a;
}
//teat8 local a+1 then return the local 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);
	///int c = test3;
	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 << test <<"---"<<test(e,f)<< "--" << test2 << "--" << test3 << "--" << ts.t() << "--" << test4 <<
		"--" << test5 << endl;
	cout << "+++++++++++++++++++++++++" << endl;*/
	
	//1.先构造test8(a)和(void*)test8(a),然后再输出cout
	cout << "&a in main:" << &a << "   " << "*test8(a):" << (void*)test8(a) << "  " << "test8(a)" << test8(a)<< endl;
	system("PAUSE");
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值