const challenge

Janice Caron caron800 at googlemail.com
Wed Jan 30 23:50:40 PST 2008


On Jan 30, 2008 10:09 PM, Jason House <jason.james.house at gmail.com> wrote:
>   const class A; // can't work while debugging

Actually, I think it can. You just have to cheat a bit:

    debug struct executionLog
    {
        const void function1(/*...*/) { /* ... */ }
        const void function2(/*...*/) { /* ... */ }
        const void function3(/*...*/) { /* ... */ }
        ...
        private:
            /* all variables */
    }

If the member functions need to modify the member variables, they will
need to cast away const. Yes, that is cheating, and undefined, but (a)
at least all the cheating is localised to one place, so the rest of
your code maintains its const correctness, and (b) the undefined
behavior will only occur in your debug build, so if works on your
platform, it really doesn't matter what it will do on anyone else's,
since they will only see the release version. (Also, (c) - it's a
struct, not a class, so all the member variables /will/ be on the
stack, so the possibility of undefined behavior is minimised anyway).



More information about the Digitalmars-d mailing list