This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 24th, 2012  |  syntax: C++  |  size: 0.64 KB  |  views: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <Windows.h>
  2. #include <iostream>
  3.  
  4. struct S{};
  5.  
  6. template <typename T>
  7. class Base
  8. {
  9. public:
  10.         Base()
  11.         {
  12.                 //Init(); // Needs to call child
  13.         }
  14.  
  15.         void Test()
  16.         {
  17.                 Init();
  18.         }
  19.  
  20.         virtual void Init() = 0;
  21. };
  22.  
  23. class Child : public virtual Base<S>
  24. {
  25. public:
  26.         virtual void Init()
  27.         {
  28.                 printf("test\n");
  29.         }
  30. };
  31.  
  32. // Test pure-virtual method (make sure it's forcing)
  33. class Child2 : public virtual Base<S>
  34. {
  35. public:
  36. };
  37.  
  38. int main()
  39. {
  40.         Child foo; // Should print "test"
  41.         Child2 foo2; // Does not compile - cannot initialize abstract class
  42.  
  43.         ((Base<S>*)&foo)->Init();
  44.         foo.Test();
  45.  
  46.  
  47.  
  48.         system("pause");
  49.         return 0;
  50. }
clone this paste RAW Paste Data