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

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

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

問題描述

我正在考慮使用 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)形?)
主站蜘蛛池模板: 91久久精品一区二区二区 | 99tv| 亚洲a在线视频 | 久久精品视频在线播放 | 亚洲高清视频在线观看 | 国产视频一区在线 | 91看片在线观看 | 狠狠骚| 亚洲欧洲视频 | 国产精品成人一区二区三区夜夜夜 | 精品国偷自产在线 | 1级黄色大片| 午夜男人免费视频 | 视频在线亚洲 | a级在线观看 | 中文字幕免费视频 | 欧美一级二级视频 | 亚洲 中文 欧美 日韩 在线观看 | 日韩在线观看一区 | 中文字幕一区二区三区精彩视频 | 成人国产精品色哟哟 | 欧美一级α片 | 美女拍拍拍网站 | 亚洲日本视频 | 欧美极品在线视频 | 久久99这里只有精品 | 欧美黑人国产人伦爽爽爽 | 欧美午夜久久 | 精品一区二区三区在线视频 | 91观看 | 伊人激情网 | 日日夜夜操天天干 | 国产精品精品久久久久久 | 国产成人啪免费观看软件 | 日本久久www成人免 成人久久久久 | 欧美视频 | 欧美成人一级 | 亚洲伊人精品酒店 | 国产一区久久 | 日韩二区 | 欧美日韩高清一区 |