久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在C++中打印成員函數(shù)地址

How to print member function address in C++(如何在C++中打印成員函數(shù)地址)
本文介紹了如何在C++中打印成員函數(shù)地址的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

看起來std::cout不能打印成員函數(shù)的地址,例如:

It looks like std::cout can't print member function's address, for example:

#include <iostream>

using std::cout;
using std::endl;

class TestClass
{
  void MyFunc(void);

public:
  void PrintMyFuncAddress(void);
};

void TestClass::MyFunc(void)
{
  return;
}

void TestClass::PrintMyFuncAddress(void)
{
  printf("%p
", &TestClass::MyFunc);
  cout << &TestClass::MyFunc << endl;
}

int main(void)
{
  TestClass a;

  a.PrintMyFuncAddress();

  return EXIT_SUCCESS;
}

結果是這樣的:

003111DB
1

如何使用 std::cout 打印 MyFunc 的地址?

How can I print MyFunc's address using std::cout?

推薦答案

我不認為該語言提供了任何用于執(zhí)行此操作的工具.operator << 有用于流打印出普通 void* 指針的重載,但成員函數(shù)指針不能轉換為 void*s.這都是特定于實現(xiàn)的,但通常成員函數(shù)指針被實現(xiàn)為一對值 - 一個指示成員函數(shù)是否為虛擬的標志,以及一些額外的數(shù)據(jù).如果函數(shù)是非虛擬函數(shù),則額外信息通常是實際成員函數(shù)的地址.如果該函數(shù)是虛函數(shù),則該額外信息可能包含有關如何索引到虛函數(shù)表中以查找給定接收者對象的函數(shù)的數(shù)據(jù).

I don't believe that there are any facilities provided by the language for doing this. There are overloads for operator << for streams to print out normal void* pointers, but member function pointers are not convertible to void*s. This is all implementation-specific, but typically member function pointers are implemented as a pair of values - a flag indicating whether or not the member function is virtual, and some extra data. If the function is a non-virtual function, that extra information is typically the actual member function's address. If the function is a virtual function, that extra information probably contains data about how to index into the virtual function table to find the function to call given the receiver object.

總的來說,我認為這意味著在不調用未定義行為的情況下打印成員函數(shù)的地址是不可能的.您可能必須使用一些特定于編譯器的技巧才能實現(xiàn)此效果.

In general, I think this means that it's impossible to print out the addresses of member functions without invoking undefined behavior. You'd probably have to use some compiler-specific trick to achieve this effect.

希望這有幫助!

這篇關于如何在C++中打印成員函數(shù)地址的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 欧美日韩淫片 | 日本一区二区三区精品视频 | 欧美日一区 | 国产精品成人一区二区三区 | 超碰520| 国产一级免费视频 | 色免费看| av电影手机版 | 日韩免费视频一区二区 | 久久精品国产久精国产 | 91精品久久久久久久久 | 蜜臀久久99精品久久久久久宅男 | 久久久精品一区二区三区 | 在线视频 中文字幕 | 欧美影院| 亚洲欧美中文日韩在线v日本 | 国产欧美一区二区三区久久人妖 | 欧美精品v国产精品v日韩精品 | 日韩三级一区 | 成人av色| 亚洲欧美综合精品另类天天更新 | 日韩午夜精品 | 久久91精品国产一区二区 | 一区二区三区国产 | 国产91中文| 国产在线观看 | 欧美高清视频在线观看 | 欧美日韩亚洲视频 | 久久久久久一区 | 午夜精品久久久久久久久久久久久 | 久久久国产一区二区三区 | 免费黄色日本 | 国产成人精品久久 | 免费同性女女aaa免费网站 | 午夜影院网站 | 亚洲日本成人 | 玖草资源| 欧洲亚洲一区二区三区 | 天天干天天谢 | 成人免费网站在线 | 成人精品鲁一区一区二区 |