日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c ++ 函数的esp指针_在C ++中通过指针访问成员函数

發布時間:2023/12/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c ++ 函数的esp指针_在C ++中通过指针访问成员函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c ++ 函數的esp指針

Create a class along with data member and member functions and then access the member functions by using a pointer in C++.

創建一個類以及數據成員和成員函數,然后使用C ++中的指針訪問成員函數。

如何通過指針訪問成員函數? (How to access a member function by pointer?)

To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this).

要通過指針訪問成員函數 ,我們必須聲明一個指向該對象的指針并將其初始化(通過在運行時創建內存,是的!我們可以為此使用新鍵盤)。

The second step, use arrow operator -> to access the member function using the pointer to the object.

第二步,使用箭頭運算符->使用指向對象的指針訪問成員函數。

Syntax:

句法:

//pointer to object declaration class_name *pointe_name; //memory initialization at runtime pointer_name = new class_name; //accessing member function by using arrow operator pointer_name->member_function();

Example:

例:

In the below example - there is a class named Number with private data member num and public member functions inputNumber(), displayNumber().

在下面的示例中-有一個名為Number的類,具有私有數據成員num和公共成員函數inputNumber()和displayNumber() 。

In the example, we are creating simple object N and a pointer to the object ptrN and accessing the member functions by using simple object N and the pointer to the object ptrN.

在該示例中,我們將創建簡單對象N和指向對象ptrN的指針,并通過使用簡單對象N和指向對象ptrN的指針來訪問成員函數。

Program:

程序:

#include <iostream> using namespace std;class Number {private:int num;public://constructorNumber(){ num=0; };//member function to get inputvoid inputNumber (void){cout<<"Enter an integer number: ";cin>>num;}//member function to display number void displayNumber(){cout<<"Num: "<<num<<endl;} };//Main function int main() {//declaring object to the class numberNumber N;//input and display number using norn objectN.inputNumber();N.displayNumber();//declaring pointer to the object Number *ptrN;ptrN = new Number; //creating & assigning memory //printing default valuecout<<"Default value... "<<endl;//calling member function with pointer ptrN->displayNumber();//input values and print ptrN->inputNumber();ptrN->displayNumber();return 0; }

Output

輸出量

Enter an integer number: 10 Num: 10 Default value... Num: 0 Enter an integer number: 20 Num: 20

Explanation:

說明:

The main three steps needs to be understood those are:

需要理解的主要三個步驟是:

  • Pointer to object creation: Number *ptrN;

    指向對象創建的指針: Number * ptrN;

  • Dynamic memory initialization to the pointer object: ptrN = new Number;

    指針對象的動態內存初始化: ptrN = new Number;

  • Accessing member function by using "Arrow Operator": ptrN->displayNumber();

    通過使用“箭頭運算符”訪問成員函數: ptrN-> displayNumber();

  • 翻譯自: https://www.includehelp.com/cpp-programs/accessing-member-function-by-pointer.aspx

    c ++ 函數的esp指針

    總結

    以上是生活随笔為你收集整理的c ++ 函数的esp指针_在C ++中通过指针访问成员函数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。