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

如何檢查對象的類型是否是 C++ 中的特定子類?

How do I check if an object#39;s type is a particular subclass in C++?(如何檢查對象的類型是否是 C++ 中的特定子類?)
本文介紹了如何檢查對象的類型是否是 C++ 中的特定子類?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!

問題描述

我正在考慮使用 typeid() 但我不知道如何詢問該類型是否是另一個類的子類(順便說一下,它是抽象的)

I was thinking along the lines of using typeid() but I don't know how to ask if that type is a subclass of another class (which, by the way, is abstract)

推薦答案

你真的不應(yīng)該.如果您的程序需要知道一個對象是什么類,這通常表明存在設(shè)計缺陷.看看你是否可以使用虛函數(shù)獲得你想要的行為.此外,有關(guān)您正在嘗試執(zhí)行的操作的更多信息會有所幫助.

You really shouldn't. If your program needs to know what class an object is, that usually indicates a design flaw. See if you can get the behavior you want using virtual functions. Also, more information about what you are trying to do would help.

我假設(shè)你有這樣的情況:

I am assuming you have a situation like this:

class Base;
class A : public Base {...};
class B : public Base {...};

void foo(Base *p)
{
  if(/* p is A */) /* do X */
  else /* do Y */
}

如果這是你所擁有的,那么嘗試做這樣的事情:

If this is what you have, then try to do something like this:

class Base
{
  virtual void bar() = 0;
};

class A : public Base
{
  void bar() {/* do X *
                
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 九色视频网站 | 色综合一区二区三区 | 欧美精品第一区 | 最新日韩在线视频 | 黄色网页在线观看 | 久久久123| 中文字幕亚洲一区二区va在线 | a在线观看免费 | 亚洲一级毛片 | 成人乱人乱一区二区三区软件 | 国产精品久久久久999 | 亚洲乱码一区二区三区在线观看 | 日韩成人免费视频 | 午夜影院普通用户体验区 | 在线四虎| 日韩欧美精品一区 | 久久伊人在| 极品国产视频 | 91在线精品视频 | 久久久精品网 | 国产精品久久久久久久久久免费看 | 成人毛片视频免费 | 欧美在线观看网站 | 精品毛片视频 | 福利精品| 国产精品二区三区在线观看 | 一级黄a | 久久久久久成人 | 男女免费观看在线爽爽爽视频 | 天天操操操操操 | 亚洲日日夜夜 | 亚洲啊v在线| 久久久精彩视频 | 一区二区三区精品视频 | 国产精品日女人 | 国产韩国精品一区二区三区 | 在线中文字幕亚洲 | 亚洲精品一区二区三区蜜桃久 | 99精品免费 | 欧美亚洲日本 | 国产激情一区二区三区 |