C++拓展笔记2-3:C++中this指针用法简介

本文深入探讨了C++中的this指针概念及其应用。详细介绍了this指针的基本原理,包括如何通过this指针访问类成员变量,以及this指针在防止变量命名冲突和实现成员函数层叠调用方面的关键作用。

this指针概览

C++中,每个类 对应了一个对象,每个对象指向自己所在内存地址的方式即为使用this指针。在类中,this指针作为一个变量通过编译器隐式传递给非暂存(non-static)成员函数。因为this指针不是对象本身,因此sizeof函数并不能用于确定this指针所对应的对象大小。this指针的具体类型与具体对象的类型以及对象是否被const关键字修饰 有关。例如,在类Employee的非常量函数中,this指针类型为Employee *,若为常量函数,则this指针类型为const Employee 。由于this本身是一个指向对象的指针,因此this这类去指针操作则得到本类中对象的地址。关于this指针的使用,举例如下:

本文代码引用和免责声明:

/**************************************************************************
 * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/

Test.h文件:

#ifndef TEST_H
#define TEST_H

class Test 
{
public:
   explicit Test( int = 0 ); // default constructor
   void print() const;
private:
   int x;
}; // end class Test

#endif /* TEST_H */

Test.cpp文件:

#include "Test.h"
#include <iostream>
using namespace std;

// constructor
Test::Test( int value ) : x( value ){}

// print x using implicit and explicit this pointers;
// the parentheses around *this are required
void Test::print() const   
{
   // implicitly use the this pointer to access the member x
   cout << "        x = " << x;
   // explicitly use the this pointer and the arrow operator
   // to access the member x
   cout << "\n  this->x = " << this->x;
   // explicitly use the dereferenced this pointer and 
   // the dot operator to access the member x
   cout << "\n(*this).x = " << ( *this ).x << endl;
} // end function print

main.cpp中的调用示例:

#include "Test.h"
int main()
{
   Test testObject( 12 ); // instantiate and initialize testObject
   testObject.print();
   return 0;
} // end main

本例中,由于this本身是指针,因此类中变量x的读写方式即为this->x。注意由于this变量是隐式传递的,因此在同一个类中的成员函数中直接调用x变量其效果等同于通过this指针调用x。使用去指针化的this变量则获得对象地址,因此通过对象地址调用变量的方式是用点号操作符。

介绍完this指针获取变量的方式之后,接下来本文将介绍this指针的两个作用。

一、this指针用于防止类中的变量冲突

this指针可以用来防止数据域与传入参数变量名相同可能导致的问题。以下列程序为例:

Time.h文件

//此处省略定义头

class Time 
{
public:
   //...此处省略若干行非重点部分
   Time &setHour( int ); // set hour
   Time &setMinute( int ); // set minute
   Time &setSecond( int ); // set second
    
    //...此处省略若干行非重点部分
private:
   unsigned int hour; // 0 - 23 (24-hour clock format)
   unsigned int minute; // 0 - 59
   unsigned int second; // 0 - 59
}; // end class Time

Time.cpp文件:

// set hour value
Time &Time::setHour( int hour ) // note Time & return
{
   if ( hour >= 0 && hour < 24 )
      this->hour = hour;
   else
      throw invalid_argument( "hour must be 0-23" );

   return *this; // enables cascading
} // end function setHour

// set minute 和 set second写法类似

此处代码传入参数名为hour,hour被赋值对象也是本类私有变量hour,此时用this指针指向hour变量的方式就防止了命名重复。注意到前述代码的返回值为指向这个对象的指针,这与接下来本文要分析的第二点有关。

二、this指针用于层叠式调用

通过返回类的去指针化的this指针*this,事实上就是返回了类所在的地址。那么此类就可以被层叠调用。如上述Time这个对象,主程序调用示例如下:

main.cpp文件:

//省略非重要的预处理指令和using命令
#include "Time.h" // Time class definition

int main()
{
   Time t; // create Time object

   // cascaded function calls
   t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );
     
    //省略其余非重要部分
} // end main

此处t.setHour其实得到的返回值为&t,那么获取setMinute的方法就是t.setMinute。同样运行t.setHour(18).setMinute(30)之后返回值仍为&t,因此可以继续调用setSecond。

那么,这样返回指向对象的类安全性有问题么?注意,此处类是以整个对象的形式被返回的,并没有出现类中的私有成员地址被返回的情况,因此返回对象地址与返回变量的地址本质是不同的。返回对象之后,对象仍然确保了私有变量的封装性,因此就变量地址造成的安全性问题,此处是不必考虑的。

参考资料:

  1. Paul Deitel & Harvey Deitel, C++11程序设计(英文版)(第2版)

转载于:https://my.oschina.net/Samyan/blog/828757

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值