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).