c++ - How to avoid downcasting while having interface and base classes? -


I'm sure I'm missing something basic here, but I can not get my head around it.

Assume that we have many possible implementations of the manager class which handles types of items base manager Based on the implementation of, they will have to set and receive specific properties from base

/ code>, therefore derived DeriveA and DerivedB What they use internally, to get the implement-specific properties, the parameter in the handle Is there a way to circumvent the need to Uncasting?

  class base {/ * abstract class with general properties]; Class DeriveA: Public Base {/ * DerivedA-Specific Properties} Category DerivedB: Public Base {/ * DerivedB-specific properties} Class IManager {/ * These functions must be implemented by each manager implementation * / public: Create virtual base * * () = 0; Virtual Zero Handle (Base *) = 0; }; Class AMANager: Public IMGenerator {Public: Create Base * (override) {New DeriveA (back); } Zero handle (base * PFU) override {// Now if we want to use the specific properties of PFU, then we need to dynamic_cast it}}; Class BMMager: Public IMGenerator {Public: Base * Create () Override {New DerivedB (back); } Zero handle (base * pubar) override {/ * here is the same;}}; Run Zero (Bool Usage) {IManager * pManager = nullptr; If (useArmager) pManager = New AManager (); Other pManager = new bmanner (); Base * pData = pManager- & gt; Create (); / * Use base specific properties ... * / pManager- & gt; Handle (padata); }  

Edit: Thanks for the valuable input, I will accept the post of @ jpo38 as it provides a possible solution to this problem. After some consideration, however, I came to know that there is an underlying problem with classroom design.

You can use this in your example, this would be:

 < Code> class DeriveA; Class DerivedB; Class Visitors {public: Virtual Zero A (a) DeriveA & amp; a = 0; Virtual ZEROB (DerivedB & amp; B) = 0; }; Class Base {Public: Virtual Zero Accepted (Visitor and Visitor) = 0; }; Class DeriveA: Public base {Public: Virtual zero accept (visitor and visitor) {visitor.visitA (* this); }}; Category DerivedB: Public Base {Public: Virtual Zero Accepted (Visitor and Visitor) {visitor.visitB (* this); }};  

Then, from AManager or BManager:

  zero handle (base * pFoo) {class MyVisitor: Public Visitor {Public: Virtual Ace & Amp; A) For any specific object, you have DeriveA} Virtual Voice visitB (DerivedB & amp; amp; amp; ;;;;;;;;;;; b;) , Which is specific to B, you have access to DerivedB}}; My visitor v; PFoo- & gt; Accept (v); }  

The disadvantage of the visitor pattern is that every time you want to be specific, you have to define a new Visitor class.

You can also consider doing this (but I definitely recommend the visitor if you later add DerivedC or want to share some specific operations via shared visual classes. Are very pleasant). public: virtual DeriveA * GetAsA) = 0; Virtual DerivedB * GetAsB () = 0; }; Class DeriveA: Public Base {Public: Virtual DeriveA * GetAsA () {This Return; } Virtual DerivedB * GetAsB () {Return NULL; }}; Category DerivedB: Public Base {Public: Virtual DeriveA * GetAsA () {Return NULL; } Virtual DerivedB * GetAsB () {This Return; }};

Then, from AManager or BManager:

  zero handle (base * pFoo) {if (pFoo-> GetAsA ()) // // use < To get rid of DownCasting for GetAsA to use DerivedA object, go to / code>   } (pFoo-> GetAsB ()) 

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -