fork download
  1. // inserted by ideone.com, for the main() function
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. // ---- ShowRunner.hpp ----
  7.  
  8. #pragma once
  9. #include <memory> //smart ptr
  10.  
  11.  
  12. struct LotsOfData // well this doesn't need to be in this header, but let's keep it brief
  13. { uint32_t a; int16_t b; int16_t c;
  14. };
  15.  
  16. class ShowRunner
  17. {
  18. public:
  19. ShowRunner();
  20. ~ShowRunner() {} // Lazy
  21.  
  22. const LotsOfData& YouMayNotPokeAroundThis();
  23.  
  24. void DoStuff();
  25.  
  26. private:
  27. std::unique_ptr< LotsOfData > m_allThatData;
  28.  
  29. // mounting the damn C++ boiler plate, isn't it just a joy
  30. ShowRunner(const ShowRunner&) = delete;
  31. void operator = (const ShowRunner&) = delete;
  32. };
  33.  
  34.  
  35. // ---- ShowRunner.cpp ----
  36. ShowRunner::ShowRunner()
  37. : m_allThatData( std::make_unique<LotsOfData>() )
  38. {
  39. // initialize some ??
  40. }
  41.  
  42. const LotsOfData& ShowRunner::YouMayNotPokeAroundThis()
  43. {
  44. return *m_allThatData; // operator overload overload! (is what I sometimes get with the C++ std lib)
  45. }
  46.  
  47. void ShowRunner::DoStuff()
  48. {
  49. m_allThatData->a = 123; // See Mom? I can write!
  50. }
  51.  
  52. int main()
  53. {
  54. ShowRunner runner;
  55.  
  56. // we are an observer of the lovely data, so we fetch a nice const ref
  57. const auto & data = runner.YouMayNotPokeAroundThis();
  58.  
  59. cout << "a is: " << data.a << endl; // I can read!
  60. cout << "writing excercise..." << endl;
  61. runner.DoStuff();
  62. cout << "a is now: " << data.a << endl;
  63.  
  64. //data.b = 567; // Mom forbade it! Does not compute!
  65.  
  66. // this warrants a whooping
  67. auto& nasty = (LotsOfData&)( data );
  68. nasty.c = 789;
  69. cout << "c is now: " << data.c << endl;
  70.  
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 4512KB
stdin
Standard input is empty
stdout
a is: 0
writing excercise...
a is now: 123
c is now: 789