testInherits.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "stdafx.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stddef.h>
  5. #ifdef _WIN32
  6. #include <windows.h>
  7. #endif //_WIN32
  8. struct IEntityFunction
  9. {
  10. virtual ~IEntityFunction()
  11. {
  12. printf("%s\n", __FUNCTION__);
  13. }
  14. virtual void f() = 0;
  15. };
  16. struct IEntityFunctionPrivilege
  17. {
  18. virtual ~IEntityFunctionPrivilege()
  19. {
  20. printf("%s\n", __FUNCTION__);
  21. }
  22. virtual void g() = 0;
  23. };
  24. struct SpEntity : public IEntityFunction
  25. {
  26. virtual ~SpEntity()
  27. {
  28. printf("%s\n", __FUNCTION__);
  29. }
  30. virtual void f()
  31. {
  32. printf("%s\n", __FUNCTION__);
  33. }
  34. };
  35. struct SpEntityPrivilege : public IEntityFunctionPrivilege, public SpEntity
  36. {
  37. virtual ~SpEntityPrivilege()
  38. {
  39. printf("%s\n", __FUNCTION__);
  40. }
  41. virtual void g()
  42. {
  43. printf("%s\n", __FUNCTION__);
  44. }
  45. };
  46. void test(IEntityFunction* pEntityFunction)
  47. {
  48. pEntityFunction->f();
  49. IEntityFunctionPrivilege *pEntityFunctionPrivilege = dynamic_cast<IEntityFunctionPrivilege*>(pEntityFunction);
  50. if (pEntityFunctionPrivilege)
  51. pEntityFunctionPrivilege->g();
  52. }
  53. int _tmain(int argc, TCHAR* argv[])
  54. {
  55. printf("\n SpEntityPrivilege:\n");
  56. IEntityFunction *pEntityFunction = new SpEntityPrivilege();
  57. test(pEntityFunction);
  58. delete pEntityFunction;
  59. printf("\n SpEntity:\n");
  60. pEntityFunction = new SpEntity();
  61. test(pEntityFunction);
  62. delete pEntityFunction;
  63. getchar();
  64. return 0;
  65. }