Creating modifiable class member variable that's directly accessible as constant from outside

Imagine that you have a class A. Is it possible to create a modifiable member variable x such that A.x can be accessed from outside the class without being able to modify it, while it can be modified from inside the class? This would save space and be more effective instead of having to create a public function for this purpose, and we could directly refer to x both inside and outside the class instead of having to call A.getX() outside the class.
Last edited on
This would save space and be more effective

This claim really needs some support. Can you provide details?

I can't really think of a reasonable way to do this. The type of a given object is independent of the location it is accessed.

The sanest solution is to use an accessor function. Another suggestion is a const reference member variable:
class A { int x_; public: int const& x = x_; };
A function has the overhead of calling the function itself. Also, by using a function, the value of x is copied over to the return statement, which is another overhead, instead of accessing the value of x directly by constant reference.
A function has the overhead of calling the function itself. Also, by using a function, the value of x is copied over to the return statement, which is another overhead, instead of accessing the value of x directly by constant reference.


If a function did nothing more than return a value, I would expect the compiler to optimise away all such overheads. There will be no function call.

https://stackoverflow.com/questions/8467134/does-compiler-optimize-this-part-of-code-const-getter
Last edited on
Hi Heymid ^_^
As mbozzi told you, there is no reason why you will ever need to do that :o) (just paraphrasing)
And most of all, it's a "functionality" that you are describing.

By the way mbozzi, the clever example you gave is very disturbing to me.
I don't really understand why the compiler gives the right to do that... for me, it's a constness violation ^_^
Anyway, the semantic is really wrong to me /o\

So, short answer is "no, you can't". You can make a variable public or "private" (not acessible) and that's all. Constness is a static "status", it cannot change dynamically in a C++ problem even for a specific class or a specific function only.

And the answer to your question is, you have a better solution. This is called an accessor, for example call it getX() and then you can call it A.getX().

> that would save some space, you said
No, what space ? where ? what kind ?
A function never take any place ever except the virtual functions.

For this getter, do it this way for example and you are good to go :
class Heymid
{
...
1
2
3
4
5
6
7
8
9
class Heymid
{
public:
	inline x_t const& getX() const
	{
		return x;
	}
	...
}

Put that definition in your header fill (hpp) and that's it.
It is exactly what you want and what you need.

I had, once, a problem like that to be solved : I wanted some classes to be able to read a "constant" value from a container class, and some others classes able to modify that value with dynamic attribution of the "rights" to modify the "constant value".
Long story... it turned out that I did exactly what I needed : a right attribution system :P

Don't think too much and be concerned about overheads using functions ;o).
A function almost always makes a code better and the C++ compilers are so powerful, it's no more an issue nowadays. Even sometimes, virtual functions are also inlined ! ^_^
It's the same thing as... is it faster to use x than a.x ?... then answer is "premature optimization is the root of all fat burgers" :oD~

Sorry for you but constness is a very critical issue in C++ ^_^ and you cannot play with it (or you can try for sure :oD. Tell me if you find something interesting ;o).
Last edited on
It's a constness violation ^_^


How so? cv-qualifications are added implicitly all the time.
I agree with mbozzi: just create a public const reference to a non-private member. Of course you have to initialize the reference in the constructor. It's much less verbose than the equivalent getter method that punksheep posted, requiring one short line to declare instead of 4, and 1 character to access (X) instead of 6 (getX())
Topic archived. No new replies allowed.