c语言指针++

Program 1:

程序1:

#include <iostream>
using namespace std;
class Test {
int VAL;
public:
Test(int v)
{
VAL = v;
}
Test* Sum(Test T1, Test T2)
{
VAL = T1.VAL + T2.VAL;
return this;
}
void print()
{
cout << VAL << " ";
}
};
int main()
{
Test T1(10);
Test T2(20);
Test* T3;
T3 = T1.Sum(T1, T2);
T1.print();
T2.print();
T3->print();
return 0;
}

Output:

输出:

30 20 30

Explanation:

说明:

Consider the sum() function, the function is taking two objects of Test class arguments and returning the pointer of the current object using this.

考虑sum()函数,该函数接收Test类参数的两个对象,并使用this返回当前对象的指针。

And, in the main() function, we created two objects T1, T2, and a pointer T3, which is holding the current object pointer returned by sum(). The sum() is adding the values of T1 and T2 and assigning in T1 because we are calling the function sum() using T1 and returning the address of T1 which is assigning to the pointer T3.

并且,在main()函数中,我们创建了两个对象T1T2和一个指针T3 ,该指针保存了sum()返回的当前对象指针。 sum()T1T2的值相加并在T1中赋值,因为我们正在使用T1调用函数sum()并返回分配给指针T3T1地址。

Program 2:

程式2:

#include <iostream>
using namespace std;
class Test {
public:
Test call1()
{
cout << "call1 ";
return *this;
}
Test call2()
{
cout << "call2 ";
return *this;
}
Test call3()
{
cout << "call3 ";
return *this;
}
};
int main()
{
Test T1;
T1.call1().call2().call3();
return 0;
}

Output:

输出:

call1 call2 call3

Explanation:

说明:

Here, we implemented a cascaded function call using this pointer, and created the class Test with 3 member functions call1(), call2(), and call3(). All these functions will return the current object of the class using *this.

在这里,我们使用此指针实现了级联函数调用,并使用3个成员函数 call1()call2()call3()创建了Test类。 所有这些函数都将使用* this返回类的当前对象。

翻译自: https://www.includehelp.com/cpp-tutorial/this-pointer-find-output-programs-set-3.aspx

c语言指针++

c语言指针++_C ++此指针| 查找输出程序| 套装3相关推荐

  1. c语言 关键字const_C ++ const关键字| 查找输出程序| 套装1

    c语言 关键字const Program 1: 程序1: #include <iostream> using namespace std; void fun(int& A) con ...

  2. g++默认参数_C ++默认参数| 查找输出程序| 套装1

    g++默认参数 Program 1: 程序1: #include <iostream> using namespace std; int sum(int X, int Y = 20, in ...

  3. 结构化程序goto语句_C ++ goto语句| 查找输出程序| 套装1

    结构化程序goto语句 Program 1: 程序1: #include <iostream> #include <math.h> using namespace std; i ...

  4. 小程序 || 语句_C ++开关语句| 查找输出程序| 套装1

    小程序 || 语句 Program 1: 程序1: #include <iostream> using namespace std; int main() {switch (printf( ...

  5. 小程序 || 语句_C ++条件语句| 查找输出程序| 套装2

    小程序 || 语句 Program 1: 程序1: #include <iostream> #include <stdio.h> using namespace std; in ...

  6. g++默认参数_C ++默认参数| 查找输出程序| 套装2

    g++默认参数 Program 1: 程序1: #include <iostream> using namespace std; int K = 10; int fun() { retur ...

  7. 小程序 || 语句_C ++条件语句| 查找输出程序| 套装1

    小程序 || 语句 Program 1: 程序1: #include <iostream> using namespace std; int main() {if (NULL) {cout ...

  8. 输入输出数组元素的函数重载_C ++函数重载| 查找输出程序| 套装3

    输入输出数组元素的函数重载 Program 1: 程序1: #include <iostream> using namespace std; class Test { public: vo ...

  9. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int A, B; fr ...

  10. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample1 { int A, B; f ...

最新文章

  1. 找Java培训机构需要注意那些
  2. sql中Cast()函数的用法
  3. PHP操作redis
  4. 扎心!天天写代码,方向真的对吗?
  5. oracle导入视图报错,exp/imp 报错处理(EXP-00003 / IMP-00019 / IMP-00058)
  6. DB2 SQL性能调优秘笈pdf
  7. Mybatis-plus批量插入、批量修改数据saveBatch等速度缓慢
  8. 大盘点!CVPR 2020目标检测论文精选
  9. TransE:Translating Embedding多元关系数据嵌入(知识图谱嵌入)2013 NIPS
  10. python 老师和父亲_父亲节丨有个当老师的爸爸是怎样的体验
  11. 小说网站的源码设计实现
  12. iPhone的屏幕尺寸和分辨率
  13. 即时通讯软件到底有哪些呢?
  14. 有一种努力叫“凌晨四点”
  15. [区块链]区块链技术在冷链运输领域的应用
  16. 简单好听的id_简单好听的贴吧id名字大全
  17. 基于飞桨实现高精度岩相自动分析,助力油气田勘探开发设计
  18. 基于Appfuse的Web应用快速开发
  19. 助力百万企业从容上云,易建科技有六大“法宝”傍身!
  20. 仿网易163邮箱界面模板

热门文章

  1. oracle目录删除后恢复,Oracle删除后不能重新安装的解决方法
  2. MUI调用原生自定义方法实现计算缓存与清空缓存
  3. Html5结合JS实现浏览器全屏功能
  4. 从0开始接触html--第一天学习内容总结
  5. 2018-05-05(在小程序中使用图标)
  6. VS2015配置内核WDK7600环境,32位下.
  7. MongoDB系列二
  8. java条件执行方法_java 开启多线程里面如何加条件判断执行其他逻辑?
  9. python计算汽车的平均油耗_用python对汽车油耗进行数据分析
  10. 华为抢购助手_华为荣耀20系列手机采用的五项新科技,科普简介