激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - C/C++ - C++中this指針用法詳解及實(shí)例

C++中this指針用法詳解及實(shí)例

2021-05-07 13:42開(kāi)源中國(guó) C/C++

這篇文章主要介紹了C++中this指針用法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下

C++this指針用法詳解及實(shí)例

概要:

本文簡(jiǎn)單介紹this指針的基本概念,并通過(guò)一個(gè)實(shí)際例子介紹this指針用于防止變量命名沖突和用于類(lèi)中層疊式調(diào)用的兩個(gè)用法。

this指針概覽

C++中,每個(gè)類(lèi) 對(duì)應(yīng)了一個(gè)對(duì)象,每個(gè)對(duì)象指向自己所在內(nèi)存地址的方式即為使用this指針。在類(lèi)中,this指針作為一個(gè)變量通過(guò)編譯器隱式傳遞給非暫存(non-static)成員函數(shù)。因?yàn)閠his指針不是對(duì)象本身,因此sizeof函數(shù)并不能用于確定this指針?biāo)鶎?duì)應(yīng)的對(duì)象大小。this指針的具體類(lèi)型與具體對(duì)象的類(lèi)型以及對(duì)象是否被const關(guān)鍵字修飾 有關(guān)。例如,在類(lèi)Employee的非常量函數(shù)中,this指針類(lèi)型為Employee ,若為常量函數(shù),則this指針類(lèi)型為const Employee 。由于this本身是一個(gè)指向?qū)ο蟮闹羔槪虼?this這類(lèi)去指針操作則得到本類(lèi)中對(duì)象的地址。關(guān)于this指針的使用,舉例如下:

本文代碼引用和免責(zé)聲明:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**************************************************************************
 * (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文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#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文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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中的調(diào)用示例:

?
1
2
3
4
5
6
7
#include "Test.h"
int main()
{
  Test testObject( 12 ); // instantiate and initialize testObject
  testObject.print();
  return 0;
} // end main

本例中,由于this本身是指針,因此類(lèi)中變量x的讀寫(xiě)方式即為this->x。注意由于this變量是隱式傳遞的,因此在同一個(gè)類(lèi)中的成員函數(shù)中直接調(diào)用x變量其效果等同于通過(guò)this指針調(diào)用x。使用去指針化的this變量則獲得對(duì)象地址,因此通過(guò)對(duì)象地址調(diào)用變量的方式是用點(diǎn)號(hào)操作符。

介紹完this指針獲取變量的方式之后,接下來(lái)本文將介紹this指針的兩個(gè)作用。

一、this指針用于防止類(lèi)中的變量沖突

this指針可以用來(lái)防止數(shù)據(jù)域與傳入?yún)?shù)變量名相同可能導(dǎo)致的問(wèn)題。以下列程序?yàn)槔?/p>

Time.h文件

//此處省略定義頭

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Time
{
public:
  //...此處省略若干行非重點(diǎn)部分
  Time &setHour( int ); // set hour
  Time &setMinute( int ); // set minute
  Time &setSecond( int ); // set second
 
  //...此處省略若干行非重點(diǎn)部分
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文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
// 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寫(xiě)法類(lèi)似

此處代碼傳入?yún)?shù)名為hour,hour被賦值對(duì)象也是本類(lèi)私有變量hour,此時(shí)用this指針指向hour變量的方式就防止了命名重復(fù)。注意到前述代碼的返回值為指向這個(gè)對(duì)象的指針,這與接下來(lái)本文要分析的第二點(diǎn)有關(guān)。

二、this指針用于層疊式調(diào)用

通過(guò)返回類(lèi)的去指針化的this指針*this,事實(shí)上就是返回了類(lèi)所在的地址。那么此類(lèi)就可以被層疊調(diào)用。如上述Time這個(gè)對(duì)象,主程序調(diào)用示例如下:

main.cpp文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
//省略非重要的預(yù)處理指令和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其實(shí)得到的返回值為&t,那么獲取setMinute的方法就是t.setMinute。同樣運(yùn)行t.setHour(18).setMinute(30)之后返回值仍為&t,因此可以繼續(xù)調(diào)用setSecond。

那么,這樣返回指向?qū)ο蟮念?lèi)安全性有問(wèn)題么?注意,此處類(lèi)是以整個(gè)對(duì)象的形式被返回的,并沒(méi)有出現(xiàn)類(lèi)中的私有成員地址被返回的情況,因此返回對(duì)象地址與返回變量的地址本質(zhì)是不同的。返回對(duì)象之后,對(duì)象仍然確保了私有變量的封裝性,因此就變量地址造成的安全性問(wèn)題,此處是不必考慮的。

感謝 閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

原文鏈接:https://my.oschina.net/SamYjy/blog/828757

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄wwww | 精精国产xxxx视频在线播放7 | 国产成人在线网址 | 国产免费一区二区三区网站免费 | 国产精品伦视频看免费三 | 九九热视频这里只有精品 | 国产精品视频亚洲 | 99re色| 国产在线精品一区二区夜色 | 亚州精品天堂中文字幕 | 久久成人免费网 | 毛片免费在线视频 | 色播视频在线播放 | 日韩视频不卡 | 91精品久久久久久 | 久久成人激情视频 | 精品亚洲成a人在线观看 | 日韩av电影免费看 | 久久综合九色综合久久久精品综合 | 久色婷婷 | 欧美一级片一区 | 久久这里只有精品1 | 91香蕉影视| 嫩呦国产一区二区三区av | 久久91久久久久麻豆精品 | 国产精品免费小视频 | 黄色片免费在线播放 | 福利在线免费视频 | 一级电影免费看 | 在线看一区二区三区 | 在线观看网址av | 日韩黄色片在线观看 | 91伊人久久| 看免费av| 男男成人高潮片免费视频欧美 | 色七七亚洲 | 久草最新在线 | 免费观看三级毛片 | 午夜视频在线看 | 久久久激情网 | 久草视频在线看 |