c++ overloading to make an overridable variable

I have a bunch of generated code. (no I can't fix the generator)
For every 'float' I want to make it so that when 'we' is set the contents of the 'float' cannot be changed.
I can't seem to find a permutation of the class OvFloat32 that will fit all of the generated code.
The permutation shown below gives me:
eldenlinux2: /.../1089-10T-MP0001_AT91 $ g++ TestOvFloat.cpp
TestOvFloat.cpp: In function ‘int main()’:
TestOvFloat.cpp:27: error: operands to ?: have different types ‘float’ and ‘OvFloat32’
TestOvFloat.cpp:28: error: operands to ?: have different types ‘OvFloat32’ and ‘float’

Is this possible?
Any Thoughts?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class OvFloat32 { //Overrideable Float
public:
    OvFloat32():val(0),we(1) {}
    //explicit OvFloat32(const float&v):val(v),we(1) {}
    OvFloat32(const float&v):val(v),we(1) {}
    //OvFloat32& operator=(const OvFloat32 val_){if(we)val=val_.val;return *this;};

    OvFloat32& operator =(const float val_){if(we) val=val_;return *this;}
    //OvFloat32& operator()(void)              {return *this;};
    operator float(void) const                 {return val;};

    float val;
    bool we; //write_enabled;
};

int main(void){
    OvFloat32 f,f2;
    //// I can't, reasonably, change these constructs 
    ///    (thousands lines of generated code)
    OvFloat32 ovf;
    OvFloat32 ovf2=9.1;
    OvFloat32 ovf3=10;
    f  = (1==1)? 0.0F : ovf;
    f2 = (1==1)? ovf :0.0F;
    ovf = (OvFloat32)f * f2;
    OvFloat32 pooled9[18]={5.0F,10.1F};
    ///////////////////////////////////////
    return 0;
}

looking over the generated code
1
2
OvFloat32 ovf2=9.1;
OvFloat32 ovf3=10;

is not right its more like
1
2
OvFloat32 ovf2=9.1F;
OvFloat32 ovf3=-10F;

So my cheesball fix is to edit the generated code with
perl -pi.org -e "s/(-{0,1}[0-9.]+F\b)/ (OvFloat32)\$1/g;" *.cpp
which seems to work....but I would really prefer to not edit the generated files!
should we not return from the overload operator.


operator float(void) const {return val;};

like

float operator float(void) const {return val;};

Soory but i do not understand what you are trying to say
Last edited on
??
'operator float(void) const ....' specifies the cast type, I don't know what adding 'float' in the return type would mean.....
g++ does not seem to understand it either....

TestOvFloat.cpp:15: error: return type specified for ‘operator float’
Ps.
$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 

Topic archived. No new replies allowed.