c++ - Deciding which derived class is passed as an argument -


I am struggling with my current design and would like to know, if there is a possibility of deciding which method to use Call without having to call a casting method that I got a base fence class.

And at the moment, my workers know how to prepare wood fences and how to prepare barb wire fences. And they can add two fences together ... but till now they only learn how to connect timber people with wooden people, and barb wired ones are wired ones.

But as they would probably learn more, there is a virtual addfence (const fence and fence) method in the fence square.

As I wanted to avoid any of the casting methods, I have special addFence methods in both classes here is an example:

  square fence {public: virtual zeros addFence (const fence and fence) = 0; ...}; Square Barb Warfens: Public fences (public: ... zero edifiers (Constrain fences and fences) {// only connect such fences, if it is a wired wired one) Zero Adfence (Constant Barifence and Fence) {// Fence Continue giving one}}; Square Wooden Fence: Public Fence [Public: ... Zero Adfence (Constant Fence and Fence) {// Simply add fences; if it is one of the woods) Zero Edense (Cont Wunderfans and Fences) One}};  

Now I would like to do something like

  fence * woodfin 1 = new woodfin (); Fence * woodfash 2 = new woodfun (); WoodenFence1- & gt; AddFence (* woodenFence2);  

But as it is a runtime decision, what type of fence I got, I have only the base fence * indicator and therefore the definition of base < Code> square fence is used in the last line.

So what's better that 'I' automatically got the type of fence? Or should I use a completely different design?

Welcome to this wonderful world.

There is no satisfactory solution to this problem. You may want to learn more about it and its more formal brother. These two things are basically identical, only a little different. In the simplest scenario, you do this:

  square wood fence; Class barbarians; Square fence {public: virtual zeros addFence (const. Fence and fence) = 0; Virtual Zero addMe (BarbWireFence and Fence) const = 0; Virtual Zero AddMe (Wooden Fencing and Fence) const = 0; ...}; Class barb Windfans: Public fences {Public: ... Zero Junk (Constant Fence and Fence) {Fence-> Ed ME (* This); } Zero addMe (BarbWireFence and Fence) const {fence- & gt; AddBarbWireFence (* this); } Zero addMe (wooden fence and fence) const {throw error ("bad fence combo"); } Zero addBarbWireFence (Constw BarbWireFence and Fence) {// Really Add Fence ...}}; Class WoodenFence: The Public Fence [Public: ... Zero addFence (Constest Fence & Fence) {Fence-> AddMe (* this); } Zero addMe (Barbwirefence and Fence) Const (throw error ("Bad Fence Combo"); } Zero addMe (wood fence and fencing) const {fence- & gt; AddWoodenFenceFence (* this); } Zero addWoodenFence (Constant Woodfun and Fence) {// Really Add Fence ...} ...};  

What will you know when you add 10 other fence types.

There may be a completely different guideline, which can template the whole business and get rid of the fence base class, because it does not provide a typed secure interface.

class BarbWireFence {public: ... zero addSimilarFence (const BarbWireFence and fence) {// continue fence with one given}}; Square wafers {public: ... add zeros Seminarfens (Cont Wundfans and Fences) {// continue fence with one given}}; Template & lt; Typename fence & gt; Zero AdWords (Fence & F1, Constant Fence & amp; F2) {F1- & gt; Adimalarfence (F2); }

Comments