How to expose a read-only integer to the outside class while being writable inside a class with C++

1 Answer

0 votes
#include <iostream>

class Test {
  int mX = 1000;

public:
  const auto& X() const { return mX; }
};

int main() {
    Test T;
    
    std::cout << T.X();
}



/*
run:

1000

*/

 



answered May 21, 2025 by avibootz
...