Composite pattern in C++ and C# - protected virtual methods -


how can add protected virtual method in "component" class, can called "composite"?

as concrete example, @ code below, , please tell me how avoid compiler error in dxcompositeshape.computesize.

abstract class dxshape // component {     public abstract void paint();     protected abstract void computesize(); }  class dxcompositeshape : dxshape // composite {     public readonly ilist<dxshape> shapes = new list<dxshape>();      public override void paint()     {         this.computesize();     }      protected override void computesize()     {         foreach (dxshape sh in shapes)         {             sh.computesize(); // compiler error cs1540         }         // , other logic here     } } 

edit: modified sample, have computesize instead of init (people assume init can called in constructor).

create non-virtual function initialise() in base class calls init

eg:

abstract class dxshape {     protected void initialise()     {         init();     }     protected abstract void init();     //... } 

as pointed out in comments below, initialise must made either public or static (only in c#), may remain protected in c++. in c++ make init private , access via calls initialise. see non-virtual interface http://en.wikipedia.org/wiki/non-virtual_interface_pattern


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -