How to check if C ++ abstract method is at runtime
class ABase{ public: virtual void do1() = 0; }; class BBase: public ABase{ public: virtual void do1(){} }; class CBase: public ABase{ public: }; ABase * base = rand() % 2 ? new BBase() : new CBase(); if(&(base->do1) != 0) base->do1();
This gives an error.
Thanks Max
Since you cannot instantiate an abstract class, any class that you encounter at runtime will not have any pure virtual methods (unless you are a constructor or destructor at that time), all of them will be overcome using clean chip. There is nothing to check.
, . , , , . CBase, .
CBase , ABase:: do1(). . , , , do1() . .
, .
, . CBase() " ".
, ( ), CBase do1() ABase.
CBase, ... , .. .
CBase
, , ABase:
ABase
class ABase { public: void do1() { /* do nothing */ } }; class BBase: public ABase { public: void do1() { /* do something */ } }; class CBase: public ABase {}; ABase * base = rand() % 2 ? new BBase() : new CBase(); base->do1();
, do1() , & ABase:: do1, & BBase:: do1 & CBase:: do1 . , & ABase:: do1 & CBase:: do1 , CBase , , , .
, CBase, , , .
"C": , NULL, , NULL, , .
Source: https://habr.com/ru/post/1777708/More articles:Is NSUserDefaults being written to disk? - objective-cHow do you feel about batch processing of poorly formatted text files? - parsingHow do you handle complex JavaScript objects on the client? - javascriptВычислять значения из блока текста на основе определенных ключей - pythonExecuting a synchronous query using $ .ajax - jqueryParsing brackets with sed using regex - pythonHow to extract corporate bond information using machine learning - parsingLinq ToDictionary returns an anonymous type - dynamicJoining sql views in oracle sql - sqlConstructor: is this the right call? - c ++All Articles