• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

Readonly Class Instance Attributes, How?

Is it possible to have a attribute of a class readonly to the outside world, but to the class itself, writeable.

I'd like to not have to implement a accssor function, but i'd also do not want to have to worry about code outside the class changeing the value of these pointers.

Is this possible??

Thanks for the help.
1 Solution
This is not possible in C++; as you suggest, the standard method to deal with this is accessor methods.
accessor methods is the way to go to solve that problem.

However, if you write the accesor method inline the compiler will often optimize it away:

class X {
   int dont_touch_this;

   int read_only() const { return dont_touch_this; }

If you then write something like:

int x = Xobj.read_only();

The compiler will treat this as if you had written:

int x = Xobj.dont_touch_this;

No function call anywhere...

Making the function inline  - as I did above - and telling the compiler to generate inline functions inline (this is normally the default for non-debug builds) does that kind of magic. One clue here is to make the accessor functions small, they should be accessor functions and not much else. The reason is that big functions tend to become regular functions even if they are defined inline.

Taking the address of a function and call it through pointer or call it through virtual mechanism also makes the function not inline for that call. However, other direct calls to the function may still be done inline without any actual funciton call taking place.

actually, it can be faked pretty closely but you'll end up doing a lot more work than writing a simple accessor and thus i don't advise it

here's a sample code that'll get you started

to make the variables read only, you simply remove operator= from the variable_t class and, you'll get compile-time read-only behavior.  if you want run-time read-only behavior, you can add a bool readonly to variable_t and test against it in operator=

note that, this is just a barebones code.  if you ever want to fully fake the thing, you'll need a lot more stuff ( just like the overloaded operator<< for streams, which is actually only needed for variable_t<std::string> in that example ) and as i said, i definitely don't advise it unless you are willing to spend some time on this

--- cut from here ---
#include <cstdlib>
#include <iostream>
#include <string>

template <typename T>
struct variable_t {

    : value() { }
  variable_t(const T& _value)
    : value(_value) { }
  operator=(T _value) {
    value = _value;
    return *this;

  operator T() { return value; }

  T value;

}; // variable_t

struct test_t {

  variable_t<int> anint;
  variable_t<double> adouble;
  variable_t<char> achar;
  variable_t<std::string> astring;

}; // test_t        

template <typename T>
operator<<(std::ostream&  _stream,
           variable_t<T>  _variable) {
  _stream << _variable.value;
  return _stream;
} // operator<<

main(int    _argc,
     char*  _argv[]) {

  test_t test;
  test.anint = 123;
  test.adouble = 1.23;
  test.achar = 'a';
  test.astring = "abc";

  int i = test.anint;
  double d = test.adouble;
  int j = test.adouble;
  char c = test.achar;
  std::string s = test.astring;
  std::cout << test.anint << " " << i << std::endl;
  std::cout << test.adouble << " " << d << " " << j << std::endl;
  std::cout << test.achar << " " << c << std::endl;
  std::cout << test.astring << " " << s << std::endl;


  return 0;

}; // main
--- cut from here ---
Fallen_KnightAuthor Commented:
Thats what i was looking for, but i was hopeing for an easier solution:)

And compile time-checking not run-tim. I would have though there was an easier way to do this. a semi private class varible or something.

But ah well, thanks for the answer!

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now