fork download
  1. #include <iostream>
  2.  
  3. class SubjectClass
  4. {
  5. public:
  6. std::size_t id() const
  7. {
  8. std::clog << "Id: " << this << std::endl;
  9. return reinterpret_cast<std::size_t>(this);
  10. }
  11. };
  12.  
  13. class Foo
  14. {
  15. SubjectClass * m_subject;
  16.  
  17. public:
  18. explicit Foo(SubjectClass * subject)
  19. : m_subject(subject)
  20. {}
  21.  
  22. std::size_t id() const
  23. {
  24. std::clog << "SubjectsId: " << this->m_subject << std::endl;
  25. return reinterpret_cast<std::size_t>(this->m_subject);
  26. }
  27.  
  28. void doShitOnWholeProgramm()
  29. {
  30. Foo f2(this->m_subject);
  31.  
  32. std::cout << "InnerSame: " << (f2.id() == this->id()) << std::endl;
  33. }
  34. };
  35.  
  36. // Gleiche Ebene hier
  37. int main()
  38. {
  39. SubjectClass subject;
  40. Foo f(&subject);
  41.  
  42. // ...
  43. f.doShitOnWholeProgramm();
  44. // ...
  45.  
  46. std::cout << "Same: " << (f.id() == subject.id()) << std::endl;
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout #stderr 0s 3340KB
stdin
Standard input is empty
stdout
InnerSame: 1
Same: 1
stderr
SubjectsId: 0xbfc4350f
SubjectsId: 0xbfc4350f
SubjectsId: 0xbfc4350f
Id: 0xbfc4350f