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:
//constructor
Number(){ num=0; };
//member function to get input
void 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 number
Number N;
//input and display number using norn object
N.inputNumber();
N.displayNumber();
//declaring pointer to the object 
Number *ptrN;
ptrN = new Number; //creating & assigning memory 
//printing default value
cout<<"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:

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

  1. Pointer to object creation: Number *ptrN;

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

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

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

  3. 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指針

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379358.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379358.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379358.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[iphone-cocos2d]分享一段Objective-C可調用的游戲中播放音樂(1)

首先需要引入AudioTool 這個framework 代碼 -(id)initWithPath:(NSString *)path{ UInt32 size, maxPacketSize; char*cookie; inti; if(gThereIsAnActiveTrack) { NSLog("*** WARNING *** GBMusicTrack only plays one track at a time…

匯編語言-010(循環移位ROL,ROR 、進位循環進位RCL,RCR 、有符號數溢出 、雙精度移位SHLD,SHRD、SHL和ADD計算 、位運算應用)

1 &#xff1a;循環移位ROL,ROR,帶進位循環進位RCL,RCR .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.code main PROC;循環左移mov al,40h ;AL 010000000brol al,1 ;AL 100000000b ,CF 0rol al,1 ;AL 000000001b ,CF 1rol al,1 ;A…

[Z]POJ 計算幾何入門題目推薦[轉PKKJ]

http://www.cnblogs.com/eric-blog/archive/2011/05/31/2064785.html http://hi.baidu.com/novosbirsk/blog/item/723a9727a9ab8804918f9dca.html其實也談不上推薦&#xff0c;只是自己做過的題目而已&#xff0c;甚至有的題目尚未AC&#xff0c;讓在掙扎中。之所以推薦計算幾何…

2013年 833c語言程序 江南大學 (A卷)

1.編寫程序實現求兩個整數最大公約數和最小公倍數. 方法一&#xff1a;輾轉相除法 算法思路&#xff1a;兩個整數a,b,其中a>b&#xff0c;求其最大公約數和最小公倍數 步驟① a%bc,其中c為余數 步驟② 若余數c為0&#xff0c;即a可以把b給整除,也就是說這里的b就是其最大公…

二十幾歲失敗的原因

1.缺乏人生目標。在研究過的人們中&#xff0c;9.98%的人沒有"人生目標"&#xff0c;這恐怕是人們失敗的最大原因。  2.自學能力不足。歷史上所謂掌握最高教育的人&#xff0c;幾乎都是"自學型"的。所謂"有教育"的人&#xff0c;不能只看成是有…

C程序生成一定范圍內的隨機數

Random numbers just numbers that lie within a range and any of the numbers can occur. 隨機數只是在一個范圍內的數字&#xff0c;任何數字都可能出現。 In programming, we come through a lot of scenarios where we need to generate random numbers. Like for dice g…

提示丟失libgcc_s_dw2-1.dll問題

QT使用MinGW編譯器編譯中的的執行文件&#xff0c;執行問題 將qt中安裝的mingw編碼器的路徑添加到環境變量path (D:\Qt\Qt5.10.1\5.10.1\mingw53_32\bin)

第1章 數據庫系統概述

第1章 數據庫系統概述 1.1 數據庫系統簡介 數據庫技術的發展歷史 人工管理階段文件系統階段數據庫系統階段

淺談多線程和異步

最近很忙&#xff0c;因此拿出時間來寫博客也算是忙里偷閑了&#xff0c;繼承前面的一貫風格&#xff0c;繼續淺談胡侃。  最近在項目中遇到了Socket異步網絡傳輸的問題&#xff0c;所以沉下心來整理下。于是&#xff0c;先問了下度娘&#xff0c;結果找到了園友志良的一篇文…

查看Sql Server的log文件大小

SELECT DB_NAME(database_id) AS DatabaseName,Name AS Logical_Name,Physical_Name, (size*8)/1024 SizeMBFROM sys.master_filesWHERE DB_NAME(database_id) AdventureWorksGO 轉載于:https://www.cnblogs.com/top5/archive/2010/03/02/1676776.html

python調用帶參函數_Python | 帶有示例的函數調用類型

python調用帶參函數There are following types of function calls in python: python中有以下類型的函數調用&#xff1a; Call by value 按價值致電 Call by reference 通過參考電話 1)按價值致電 (1) Call by value ) When, we call a function with the values i.e. pass …

ffmpeg 命令添加文字水印

使用ffplay 預覽一下效果&#xff1a; ffplay -i cctvhttp.flv -vf “drawtextfontsize100:fontfileArial.ttf:tex t‘hello world’:x20:y20:fontcolorblue:alpha0.5” -x 640 -y 480 使用ffmpeg保存為文件 &#xff1a; ffmpeg -i cctvhttp.flv -vf “drawtextfontsize10…

jquery彈出層

這是一個彈出層的插件&#xff0c;有時候做東西的&#xff0c;經常會用到了&#xff0c;所以在次發一下&#xff0c;和大家分享一下&#xff01; [task]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/x…

MUL與IMUL區別(微機原理與接口技術 第2版)課后習題3.14、P123

MUL與IMUL的詳細區別 乘數位數隱含的被乘數乘積的存放位置舉例8位ALAX中MUL BL16位AXDX與AX中&#xff08;DX存放高16位、AX存放低16位&#xff09;MUL BX 課本P97例題 一&#xff09;、將以下指令中的立即數看作是無符號數實現相乘: MOV AL,0B4H ;ALB4H180 解釋以下&…

SDL_main導致main找不到入口

SDL main的錯誤 引用SDL.h就會報這個錯誤 因為SDL 將main 宏定義為 SDL_main,所以會找不到main入口 可以使用#undef main取消這個宏定義

Java MathContext類| hashCode()方法與示例

MathContext類的hashCode()方法 (MathContext Class hashCode() method) hashCode() method is available in java.math package. hashCode()方法在java.math包中可用。 hashCode() method is used to get the hash code value of this MathContext. hashCode()方法用于獲取此M…

實驗8 SQL Server 的存儲過程

實驗8 SQL Server 的存儲過程一、實驗目的 1.掌握使用T-SQL編程的方法 2.掌握使用T-SQL語句創建一個存儲過程并驗證 3.掌握創建和執行帶參數的存儲過程 4.熟練使用系統存儲過程、系統函數 二、實驗要求 1.創建一個不帶參數的存儲過程。 2.創建一個帶參數的存儲過程p_count。 三…

Oracle ——如何確定性能差的 SQL

http://www.toadworld.com/KNOWLEDGE/KnowledgeXpertforOracle/tabid/648/TopicID/TSQ7/Default.aspx 本文主要說明在應用程序內書寫和調優 SQL 語句。假設&#xff0c;你已經知道你應用程序中的哪些 SQL 語句需要注意。事實上&#xff0c;這不太容易。那么&#xff0c;我們如何…

C#中的委托和事件(續)

http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html 歡迎瀏覽本文的后續文章&#xff1a; C#中的委托和事件(續)PDF 瀏覽&#xff1a;http://www.tracefact.net/Document/Delegates-and-Events-in-CSharp.pdf文中代碼在VS2005下通過&#xff0c;由于VS2003(.Ne…

Java LocalDate類| minusYears()方法與示例

LocalDate類minusYears()方法 (LocalDate Class minusYears() method) minusYears() method is available in java.time package. minusYears()方法在java.time包中可用。 minusYears() method is used to subtract the given years from this LocalDate and return the LocalD…