📜 ⬆️ ⬇️

Overriding standard types in C ++

Often, programmers, for convenience and brevity, override the simplest data types and 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 will make a sentence: why not redefine the simplest types by writing a class for each type? Memory consumption when storing such objects should not increase (the essno, we do not use virtual functions). For example, we write class Double, 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 the 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, it is possible to work with the simplest types as with objects, improve 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, it is enough to put the counter in an overloaded operator).

In terms of performance / memory overhead, there should be no overhead.

So far I can not share the pros / cons of this approach, but gradually integrate it into my C ++ libraries.

Source: https://habr.com/ru/post/56264/


All Articles