« Previous entry | Next entry » Browse > Snippets

Skip to comments (15) readonly C++ class member
Posted by Erik on Jan 14 2006 @ 16:39  :: 4108 unique visits

Sometimes you want a class member which can only be read by the outside. Most of the time this is done with a simple get... function. Here is another simpler way to do this:
CODE: CPP
class someclass {
private:
  int _themember;
public:
  const int &themember;

  someclass() : _themember(0), themember(_themember) {
    ;
  }

  void somefunction() {
    _themember = 42;
  }
};


Inside the class you can use _themember to write and read from.
The outside can only read themember:
CODE: CPP
someclass foo;

int a = foo.themember; // works

foo.themember = 1337; // error
 

15 comments posted so far
Add your own »

1. On Jan 14 2006 @ 21:23 guest wrote:

The problem with this is that it is completely unintuitive for someone reading the code.  It also violates the principles of data encapsulation; if you're going to use to make data private, you should do it so that it's obviously private.

2. On Jan 14 2006 @ 22:11 Erik wrote:

It isn't a way to make data private, it is a way to make data readonly. It perfectly fits into the principles of data encapsulation. and why shouldn't it be intuitive?

3. On Jan 15 2006 @ 22:08 Revvy wrote:

It's not intuitive because someone else looking at the code would assume that if it can be read like a public variable, it would follow that it can be also be written to like a public variable.

4. On Jan 20 2006 @ 16:44 hxa7241 wrote:

It takes twice the memory!

And, as mentioned, it breaks encapsulation. If you wanted to change the representation of _themember -- to float, say -- you would run into extra work and awkwardness...

5. On Feb 15 2006 @ 00:20 Shalom wrote:

1. It doesn't use twice the memory, the reference "themember" is given the same memory address as the int "_themember" -- and if the compiler is really smart, this gets done during link-time (As long as you don't do something really special, like re-cast "themember" and use it in some kind of l-value expression.)

2. I don't really like to discuss philosophy in C/C++ - my attitude is more of: "if you can do it and it's defined in the language spec, it's supposed to do what you told it to." So Please take the next comment with a grain of salt.

I don't think there's any difference between this declaration of a "changble const" that's public, and the actual "plain-vanialla" public const. If there's a constant in a class, it's a constant. If it's public, then the user of that class can read it. It doesn't expect to be able to change it, but since code of that user's makes use of the const's value, it shouldn't care if the value changes. It should simply keep working as if the new value has always been there (or not. depends on what you're doing.)

But I have to admit one big reservation: most compilers are far more competent at optimizing-away calls to value-getting-functions (even if they aren't inline) than correctly interpreting the above. You much more likely to have any call to such a function simply replaced with a simple stack push of the returned value, i.e. the value of _themember.

6. On Mar 07 2006 @ 19:14 hxa7241 wrote:

1. That's an optimization. Refs must in general be pointers. I wouldn't trust it unless I had: either the clause in the standard guaranteeing it (I don't think there is one), or results from compilers showing it is common.

7. On Mar 10 2006 @ 15:27 guest wrote:

Well, should this class be non-copyable or the default copy behavior is intentional? What about the copy constructor?

8. On Aug 14 2006 @ 18:08 guest wrote:

how, if the member is a vector and i want every element in the vector to be readonly by outside?

if i use "get()" to do this, will the performance be very poor?

9. On Dec 08 2008 @ 05:23 guest wrote:

I think trick is very good!!!!!!

10. On May 19 2009 @ 09:33 guest wrote:

Find louis vuitton handbag and louis  Louis Vuitton Handbags , louis vuitton wallet and louis vuitton purse items on Louis Vuitton Store Browse a huge selection of LV Handbags Louis Vuitton is luxury gifts, French fashion, the replica Louis Vuitton Store is woman best friend. With Louis Vuitton Discover real Louis Vuitton bags, Vuitton accessories and the latest handbag lines. Join the biggest active Louis Vuitton enthusiast community on the web today

UGGs is a brand that is all about luxury and comfort for everyday life. Only the finest quality materials are used to create UGG Boots. Provide UGG Women Boots,UGG Man Boots,UGG Kids Boots.
UGG Australia is the largest distributor of Grade-A sheepskin.
Find Women's UGGs, Men's UGG Boots, and Kids UGGs on Sale all made with ... UGG Store include Discount UGG  UGG Classic Tall,UGG Classic Short

Louis Vuitton is luxury gifts, French fashion, the replica Louis Vuitton Handbag is woman best friend.Monogram Groom.
Offers Discount Louis Vuitton handbags and Louis Vuitton bags and all other designer handbags,free global fast shipping,low price and top quality.Monogram Jokes,Monogram Suede cheap Louis Vuitton
Louis Vuitton.

Looking For Gucci Shoes ? Gucci Store provide gucci Mens shoes,gucci Womens shoes
Wonderful Gucci shoes sale Gucci men's shoes and Gucci women's shoes at discount Gucci Shoes prices.
cheap gucci Shoes
Gucci Shoes and gucci clothing Spring - Summer 2009, Prada Shoes and prada clothing from the Latest Collection 2009 and Dolce Gabbana Clothing 2009
Gucci Loafers
Gucci Sneakers

UGGs
Louis Vuitton Handbags
Gucci Shoes
Louis Vuitton
UGG Boots
Louis Vuitton Handbags
gucci shoes
Monogram Groom
Discount Louis Vuitton
UGG Boots
Louis Vuitton handbags

11. On Jun 12 2009 @ 10:05 guest wrote:

i know this thread is old...but think abut that:
every constant reference can be easily converted to a non-constant reference which gives full access to the private data members:
CODE: CPP
int main(int argc,char *argv[]) {
    someclass object;
    int & full_access_member=(int &)object.themember;
    full_access_member=42;
}

so, no real read-only;D

13. On Aug 29 2009 @ 12:06 guest wrote:

good!!!!!!!!!!!!

14. On Sep 17 2009 @ 10:03 Steffen wrote:

I prefer using C#-style properties without those get/set keywords in c++ too!

@Jun 12 2009:
Access to memory-addresses is one of the major powers
or weaknesses (...as some consultants say) of c++.
Your ptr-cast would also work for a public member variable
declared as "const int iVal".

15. On Jan 05 2010 @ 14:27 uggbaileybutton wrote:

bailey button uggs

-ugg boots cheap

ugg boots uk

ugg classic

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: