testInherits.cpp 1.3 KB

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