?
Solved

Readonly Class Instance Attributes, How?

Posted on 2003-02-19
4
Medium Priority
?
309 Views
Last Modified: 2010-04-01
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.
Fallen
0
Comment
Question by:Fallen_Knight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Expert Comment

by:Managu
ID: 7985745
This is not possible in C++; as you suggest, the standard method to deal with this is accessor methods.
0
 
LVL 12

Expert Comment

by:Salte
ID: 7986280
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 {
private:
   int dont_touch_this;

public:
   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.

Alf
0
 
LVL 7

Accepted Solution

by:
burcarpat earned 100 total points
ID: 7987011
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 {

  variable_t()
    : value() { }
   
  variable_t(const T& _value)
    : value(_value) { }
 
  variable_t<T>&
  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>
std::ostream&
operator<<(std::ostream&  _stream,
           variable_t<T>  _variable) {
  _stream << _variable.value;
  return _stream;
} // operator<<


int
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;

  system("PAUSE");

  return 0;

}; // main
--- cut from here ---
0
 

Author Comment

by:Fallen_Knight
ID: 7990625
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!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question