How to simulate virtuality for method template

后端 未结 5 1044
忘了有多久
忘了有多久 2021-01-19 14:08

I have a class hierarchy where I want to introduce a method template that would behave like if it was virtual. For example a simple hierarchy:

class A {
  vi         


        
5条回答
  •  旧时难觅i
    2021-01-19 14:38

    Is there any common code you could extract and make virtual?

    class A {
      virtual ~A() {}
    
      template
      void method(T &t) 
      {
          ...
          DoSomeWork();
          ...
      }
    
      virtual void DoSomeWork() {}
    };
    
    class B : public A {
      virtual void DoSomeWork() {}
    };
    

提交回复
热议问题