C++中的临时对象(http://blog.chinaunix.net/u/11680/showart_316503.html)
来源:互联网 发布:php 报表管理系统 编辑:程序博客网 时间:2024/06/07 09:14
一,以值的方式给函数传参。
/*-----------------------
Platform:WinXp + VC6
-----------------------*/
#include <stdio.h>
class CTemp
{
public:
int a;
int b;
public:
CTemp(CTemp& t){ printf("Copy function!/n");a = t.a;b = t.b;};
CTemp(int m = 0,int n = 0);
virtual ~CTemp(){};
public:
int GetSum(CTemp ts);
};
CTemp::CTemp(int m , int n)
{
printf("Construct function!/n");
a = m;b=n;
printf("a = %d/n",a);
printf("b = %d/n",b);
}
int CTemp::GetSum(CTemp ts)
{
int tmp = ts.a + ts.b;
ts.a = 1000; //此时修改的是tm的一个副本
return tmp;
}
//--------------Main函数-----------------
void main()
{
CTemp tm(10,20);
printf("Sum = %d /n",tm.GetSum(tm));
printf("tm.a = %d /n",tm.a);
}
--------------------------------------------------------
a = 10
b = 20
Copy function!
Sum = 30
tm.a = 10
int CTemp::GetSum(CTemp& ts)
{
int tmp = ts.a + ts.b;
ts.a = 1000; //此时通过ts这个引用参考(refer to)对象本身
return tmp;
}
a = 10
b = 20
Sum = 30
tm.a = 1000
void main()
{
CTemp tm(10,20),sum;
sum = 1000; //调用CTemp(int m = 0,int n = 0)构造函数
printf("Sum = %d /n",tm.GetSum(sum));
}
-----------------------------------------------------------
a = 10
b = 20
Construct function!
a = 0
b = 0
Construct function!
a = 1000
b = 0
Sum = 1000
void main()
{
CTemp tm(10,20);
CTemp sum = 1000;
printf("Sum = %d /n",tm.GetSum(sum));
}
a = 10
b = 20
Construct function!
a = 1000
b = 0
Sum = 1000
#include <stdio.h>
class CTemp
{
public:
int a;
public:
CTemp(CTemp& t) //Copy Ctor!
{
printf("Copy Ctor!/n");
a = t.a;
};
CTemp& operator=(CTemp& t) //Assignment Copy Ctor!
{
printf("Assignment Copy Ctor!/n");
a = t.a;
return *this;
}
CTemp(int m = 0);
virtual ~CTemp(){};
};
CTemp::CTemp(int m) //Copy Ctor!
{
printf("Construct function!/n");
a = m;
printf("a = %d/n",a);
}
CTemp Double(CTemp& ts)
{
CTemp tmp; //构建一个临时对象
tmp.a = ts.a*2;
return tmp;
}
//-------------Main函数-----------------
void main()
{
CTemp tm(10),sum;
printf("/n/n");
sum = Double(tm);
printf("/n/nsum.a = %d /n",sum.a);
}
a = 10
Construct function!
a = 0
Construct function!
a = 0
Copy Ctor!
Assignment Copy Ctor!
sum.a = 20
CTemp Double(CTemp& ts)
{
return ts.a*2;
}
/*--------上面的代码相当于-------
CTemp _ret
void Double(CTemp& ts)
{
_ret.a = ts.a*2;
}
---------------*/
//---------Main函数-----------
void main()
{
CTemp tm(10);
printf("/n/n");
CTemp sum = Double(tm);
printf("/n/nsum.a = %d /n",sum.a);
}
a = 10
Construct function!
a = 20
sum.a = 20
- C++中的临时对象(http://blog.chinaunix.net/u/11680/showart_316503.html)
- 在windows下通过VC6.0第一次用C调用SQLite http://blog.chinaunix.net/u/16292/showart_198806.html
- http://blog.chinaunix.net/uid/324202.html?
- http://blog.chinaunix.net/uid/9688646.html
- XPath介绍(转自http://blog.chinaunix.net/u/24129/showart_311035.html)
- Qemu 中文手册 (转自:http://blog.chinaunix.net/u/12679/showart_432398.html)
- 数据库的静默和挂起状态(http://blog.chinaunix.net/u/22151/showart_1165163.html)
- getopt函数的使用http://blog.chinaunix.net/u/12592/showart_429841.html
- 长短字符串问题(转载自http://blog.chinaunix.net/u/25675/showart_261303.html)
- sed命令详解 转载:http://blog.chinaunix.net/u/22677/showart_1076318.html
- C语言中的可变参数函数 三个点“…” http://blog.chinaunix.net/uid-7283526-id-2198861.html
- 个人BLOG:http://blog.chinaunix.net/uid/20680966.html
- windows进程中的内存结构http://blog.chinaunix.net/u2/66119/showart_548336.html
- linux常用c函数 进程操作篇 http://blog.chinaunix.net/uid-25906157-id-3136757.html
- 关于控制台程序输出重定向到IDE两个例子 之一 http://blog.chinaunix.net/u/26972/showart_236893.html
- struct的初始化,拷贝及指针成员的使用技巧(摘自http://blog.chinaunix.net/u/9577/showart_417268.html)
- fckeditor的用法(转)里面有具体例子(http://blog.chinaunix.net/u/16215/showart_214196.html)
- GTK+图形化应用程序开发学习笔记(一)—概述 转载:http://blog.chinaunix.net/u/22935/showart_308184.html
- KXmlParser 中文乱码问题
- 关于如何做人
- 使用CDHtmlDialog ,实现javascript 调用C++
- web兼容总汇--ie vs FF
- 再谈编码(很多都是拾人牙慧不要见笑)
- C++中的临时对象(http://blog.chinaunix.net/u/11680/showart_316503.html)
- QTableWidget的简单使用
- Python-String-Function
- MMI_TASK相关
- 安全删除U盘
- vi 命令
- Linux wc命令详解
- 让tomcat支持https/ssl
- CSS Hack 汇总