博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ this Pointer implicit parameter
阅读量:5984 次
发布时间:2019-06-20

本文共 1131 字,大约阅读时间需要 3 分钟。

Every object in C++ has access to its own address through an important pointer called this pointer.

The this pointer is an implicit parameter to all member functions.

Therefore, inside a member function, this may be used to refer to the invoking object.

 

Friend functions do not have a this pointer, because friends are not members of a class.

Only member functions have a this pointer.

Let us try the following example to understand the concept of this pointer −

#include 
using namespace std; class Box { public: // Constructor definition Box(double l = 2.0, double b = 2.0, double h = 2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; } double Volume() { return length * breadth * height; } int compare(Box box) { return this->Volume() > box.Volume(); } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; int main(void) { Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 if(Box1.compare(Box2)) { cout << "Box2 is smaller than Box1" <

转载于:https://www.cnblogs.com/poission/p/10874337.html

你可能感兴趣的文章
iOS 计算两个日期字符串的差值
查看>>
UTF-8 编码及检查其完整性
查看>>
由一条微博引发的 — Xcode LLDB 调试断点总结
查看>>
Android NDK开发扫盲及最新CMake的编译使用
查看>>
Weex开发系列(一):初识Weex
查看>>
开源 UI 库中,唯一同时实现了大表格虚拟化和树表格的 Table 组件
查看>>
找到思聪王
查看>>
[译] 学习 Spring Security(五):重发验证邮件
查看>>
快速的React Native开发方法
查看>>
rabbitmq中文教程python版 - 工作队列
查看>>
SpringBoot 1024行代码 - Eureka Server
查看>>
Docker和rkt快别争了,k8s才是容器生态的中心
查看>>
服务器时区问题
查看>>
JAVA反射技术应用-ReflectUtil
查看>>
removeGeneratedClassFiles Failed
查看>>
nagios安装全攻略
查看>>
Perl进阶知识点(2)
查看>>
Android adb.exe 启动失败
查看>>
我的友情链接
查看>>
使用JavaMail完成邮件的编写
查看>>