c++ - Can the virtual function be found at runtime without calling? -
i have base class base, many derived classes (eg. derived1, derived2). base has pure virtual function fn, called many times using base pointer. every time function called, need logging , related stuff. in particular, use boost_current_function in derived-class functions find out function called. there way know information before calling function, not have rewrite bookkeeping code in every derived function?
edit: wish avoid writing __pretty_function__ in each derived function.
#include <iostream> using namespace std; class base { public: virtual void fn() = 0; }; class derived1:public base { public: void fn() { cout<<__pretty_function__<<endl; } }; class derived2:public base { public: void fn() { cout<<__pretty_function__<<endl; } }; int main() { int choice =0; base *ptr1 = nullptr; cout<<"choose 0/1: "<<endl; cin>>choice; if(choice == 0) { ptr1 = new derived1; }else { ptr1 = new derived2; } //********can write here, give same result? ptr1->fn(); }
no, cannot be. c++ not support kind of introspection. __pretty_function__ you're gonna get.
Comments
Post a Comment