c++ - Should I stop using abstract base classes/interfaces and instead use boost::function/std::function? -


i've learned std::function , used , have question: have delegates, , when should use abstract base classes , when, instead, should implement polymorphism via std::function objects fed generic class? did abc receive fatal blow in c++11?

personally experience far switching delegates simpler code creating multiple inherited classes each particular behaviour... little confused abotu how useful abstract bases on.

prefer defined interfaces on callbacks

the problem std::function (previously boost::function) of time need have callback class method, , therefore need bind this function object. in calling code, have no way know if this still around. in fact, have no idea there this because bind has molded signature of calling function caller requires.

this can naturally cause weird crashes callback attempts fire methods classes no longer exist.

you can, of course use shared_from_this , bind shared_ptr callback, instance may never go away. person has callback participates in ownership without them knowing it. want more predictable ownership , destruction.

another problem, if can callback work fine, callbacks, code can too decoupled. relationships between objects can difficult ascertain code readability becomes decreased. interfaces, however, provide compromise between appropriate level of decoupling specified relationship defined interface's contract. can more specify, in relationship, issues owns whom, destrcution order, etc.

an additional problem std::function many debuggers not support them well. in vs2008 , boost functions, have step through 7 layers function. if other things being equal callback best choice, sheer annoyance , time wasted accidentally stepping on target of std::function reason enough avoid it. inheritance core feature of language, , stepping overridden method of interface instantaneous.

lastly i'll add we don't have delegates in c++. delegates in c# core part of language, inheritance in c++ , c#. have std library feature imo 1 layer removed core language functionality. not going tightly integrated other core features of language. instead helps formalize idea of function objects have been c++ idiom while now.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -