A Psuedo-Const Type ???
Heres what I want to do... I want to be able to assign a value to a variable from another variable once and then prevent it from ever being changed again..
Basically something like..
const int x;
int a;
//...
x = a;
and then have x act like a regular const
However - I know this can''t be done with const (its not defined an inital value)
The only half-way easy solution I can think of for this is to have a class that overloads all the operators and acts like a const that can be set by a variable only once..
Though this seems almost trivial to implement I just wish there was a simpler solution..
My Question - Is there one?
There are no direct support for this in C/C++.
Why would you want to do such a thing?
Why would you want to do such a thing?
Well, you could abuse the preprocessor to guard your value.
ex:
#define DECL(type, var) type _ ## var; int __ ## var ## _set = 0;
#define SET(var, value) ( __ ## var ## _set == 0 && ( _ ## var = value, __ ## var ## _set++))
#define GET(var) ((const)(_ ## var))
Then in your code:
DECL(int, x);
int a;
SET(x, a);
a *= GET(x);
GET(x) = a; // fails because ((const)(_x)) isn''t a valid lvalue.
SET(x, a); // no-op because _x already has a value assigned.
ex:
#define DECL(type, var) type _ ## var; int __ ## var ## _set = 0;
#define SET(var, value) ( __ ## var ## _set == 0 && ( _ ## var = value, __ ## var ## _set++))
#define GET(var) ((const)(_ ## var))
Then in your code:
DECL(int, x);
int a;
SET(x, a);
a *= GET(x);
GET(x) = a; // fails because ((const)(_x)) isn''t a valid lvalue.
SET(x, a); // no-op because _x already has a value assigned.
I think you could try:
const int x;
int a;
int& y = const_cast<int>( x );
y = a;
but it's evil...
Edited by - SteveC on 2/17/00 10:29:23 AM
const int x;
int a;
int& y = const_cast<int>( x );
y = a;
but it's evil...
Edited by - SteveC on 2/17/00 10:29:23 AM
SteveC is on the right path of doing it. This is how it''s declared withing ANSI C++ standards. In theory you don''t want to change a contants, but sometimes you need too, and this is why the constant cast is within the language
...
const int a = 0;
// a = 0 and contant
a = const_cast(5);
// a = 5 and still constant
...
ZaneX
...
const int a = 0;
// a = 0 and contant
a = const_cast(5);
// a = 5 and still constant
...
ZaneX
ZaneX:
Does that really work?
where you placed the const cast fixes the const-ness but ''int a'' is still an l-value, and a const one at that.
And aren''t you not supposed to be able to assign to a const l-value ?
I didn''t actually try compiling what I thought of, so it could be wrong too![](wink.gif)
Does that really work?
where you placed the const cast fixes the const-ness but ''int a'' is still an l-value, and a const one at that.
And aren''t you not supposed to be able to assign to a const l-value ?
I didn''t actually try compiling what I thought of, so it could be wrong too
![](wink.gif)
forgot something... It should be like this...
...
int a = conts_cast(5);
...
Now it should work fine.
...
int a = conts_cast(5);
...
Now it should work fine.
Actually, What I wrote was fine, Stupid html tags... let''s see if I can get it this time...
a = const_cast<>(5);
a = const_cast<>(5);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement