Advertisement
Guest User

Untitled

a guest
Jul 26th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. template <class T>
  5. void Output(const char * Str, T Func)
  6. {
  7. void *Ptr = reinterpret_cast<void *>(Func);
  8. std::ptrdiff_t Num = reinterpret_cast<std::ptrdiff_t>(Ptr);
  9. std::cout << Str << " " << Num << std::endl;
  10. }
  11.  
  12. class TAnotherBase { long a[10]; };
  13.  
  14. struct TBase {
  15. typedef void (TBase::*TFunc)();
  16. TFunc Func;
  17. TBase(TFunc F) {
  18. Func = F;
  19. Output("Ctor TBase ", Func);
  20. }
  21. void CallF() {
  22. std::cout << "This in TBase: " << typeid(this).name() << " " << this << std::endl;
  23. (this->*Func)();
  24. }
  25. };
  26.  
  27. struct TDerived: public TAnotherBase, public TBase {
  28. TDerived(): TBase(static_cast<TBase::TFunc>(&TDerived::F)) {
  29. Output("Ctor TDerived ", &TDerived::F);
  30. CallF();
  31. }
  32. void F() {
  33. std::cout << "This in TDerived::F: " <<typeid(this).name() << " " << this << std::endl;
  34. }
  35. };
  36.  
  37. int main(int argc, char **argv) {
  38. TDerived Derived;
  39. return 0;
  40. }
  41.  
  42. Ctor TBase 4197502 (1)
  43. Ctor TDerived 4197502 (2)
  44. This in base: P5TBase 0x7fff6b30fc00 (3)
  45. This in TDerived::F: P8TDerived 0x7fff6b30fbb0 (4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement