c++全局函数 && 成员函数
2024-08-26 22:40:34
#include<iostream>
using namespace std;
class Test {
public:
Test(int a=, int b=)
{
this->a = a;
this->b = b;
}
Test & add_menber(Test &t2)
{
this->a += t2.a;
this->b += t2.b;
return *this;
}
void printf(Test &t)
{
cout << "a="<<t.a << "b=" << t.b << endl;
}
~Test()
{ }
//private://全局函数如果不是友元函数不能访问私有属性
int a;
int b; };
Test add(Test &t1, Test &t2)
{
Test temp;
temp.a= t1.a + t2.a;
temp.b = t1.b + t2.b;
return temp;
}
int main()
{
Test t1(, ), t2(, ), t3;
t3 = add(t1, t2);
cout << "t3.a="<<t3.a<<"t3.b="<<t3.b << endl; Test t4(, ),t5(,);
t4.printf(t4.add_menber(t5));
cout << "hello world!\n";
return ;
}
summary:
非静态成员函数有this指针,这样的成员函数实现参数会比全局函数少一个,反之全局函数参数会比非静态成员函数多一个。c++中多使用引用更好。
最新文章
- SpringMVC下Ajax请求的方法,@Responsebody如果返回的是布尔值,ajax不会接到任何回传数据
- 创建文本注记TextElement
- WNDR3700V4 安装SVN Server
- Redis简介
- js面向对象组件
- 网站压力测试工具webbench
- Visual studio 2013的安装和单元测试
- solr5.5教程-tomcat布署
- 《APUE》第四章笔记(3)
- Java序列化技术
- 如何修复在Microsoft Azure中“虚拟机防火墙打开,关闭RDP的连接端口”问题
- 100个精选zencart扩展插件
- SDN学习之实现环路通信
- 应用教程之帕克西AR虚拟试妆3D动态美妆
- MyBatis_关联关系查询
- Uva 12171 Sculpture - 离散化 + floodfill
- HTTPS介绍
- JS _函数作用域及变量提升
- Emacs中多个golang项目的配置方法
- Openldap命令详解