Overriding Standard Types in C ++

    Often, programmers redefine the simplest data types for convenience and brevity, give them other names. Most often this is done like this:

    typedef unsingned int uint32;
    typedef signed int int32;
    typedef unsigned char byte;

    Well and so on. I’ll make a proposal: why not redefine the simplest types by writing a class for each type? The memory consumption when storing such objects should not increase in theory (essno, we do not use virtual functions). For example, we write the Double class, something like this:

    class Double
    {
    public:
         Double (const Double & value);
         Double operator + (const Double & right) const;
         Double operator- (const Double & right) const;
         Bool IsPositiveInfinity ();
         Bool IsNegativeInfinity ();
    private:
         double _value;
    }
    



    Or for example for type char:

    class char
    {
    public:
        Char (char value);
        Bool IsDecimalDigit () const;
        Bool IsLetter () const;
        Bool IsWhiteSpace () const;
        Bool operator == (const Char & other);
        Bool operator! = (Const Char & other);
    private:
        char _value;
    };
    


    Thus, you can work with the simplest types as objects, increase the readability of the code, have more control over what happens in the program (for example, to count the number of multiplications performed on the double type, just put the counter in the overloaded operator).

    In theory, there should not be an overhead in terms of performance / memory consumption.

    So far I can’t share the pros / cons of this approach, but I am gradually introducing it into my C ++ libraries.

    Also popular now: