Ilya_Bykonya

C++ get private member

Aug 10th, 2024
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | Source Code | 0 0
  1. #include <iostream>
  2.  
  3. template<class M>
  4. struct public_cast {
  5.     static inline M m{};
  6. };
  7. template<auto M>
  8. struct access {
  9.     static const inline auto m = public_cast<decltype(M)>::m = M;
  10. };
  11.  
  12. class C {
  13. private:
  14.     int x = 42;
  15. };
  16. template struct access<&C::x>;
  17.  
  18. int main() {
  19.     std::cout << C{}.*public_cast<int C::*>::m;
  20. }
  21.  
Tags: cpp
Advertisement
Add Comment
Please, Sign In to add comment