c++ - Creating custom or using built in types -


in projects people create custom types everything, , in others use ints , floats represent temperatures, lengths , angles.

i can see advantages , draw backs both, , guess depends on type of project working on if idea or not create these kinds of types.

here i'm thinking of:

class someclass {     physics::temperature temperatureonmoon(geometry::distance distancefromsun);      geometry::area shadow(geometry::angle xangle, geometry::angle yangle, geometry::triangle triangle); }; 

the temperature type have fahrenheit() , celsius() method, area type have constructor takes 2 point types , on.

this of gives great type safety , think increases readability, creates lot of dependencies. uses someclass has include these other headers , have lot more work when creating unit tests. takes time develop types.

the approach using built in types simpler use , have fewer dependencies:

class someclass {     double temperatureonmoon(double distancefromsun);      double shadow(double xangle, double yangle, double triangle); }; 

my question is, degree create these kinds of types? prefer them in larger projects? there ready made libraries kind of stuff?

i avoid creating new types when it's unnecessary. here few issues have deal with:

  1. it hides information precision - in case of distance, can distance be? integer, float double?
  2. you have problems using standard libraries - example in case of can use max(distance1, distance2)? how sorting distances? have create compare function explicitly. depends on how define type. if it's typedef of primitive type, may not need create new compare function or max function. still confusing. if distance class or struct, have overload operators explicitly, + - = *.....

  3. since don't know if it's floating point type or integer don't know if can safely use == compare 2 distances. can floating points, , if manipulated differently may end different result in theory due precision issues.

  4. the number of files maintain going bigger, building process unnecessary longer.

i create new types if don't make sense primitives @ all, , want overload operators or not allow some. i'm struggling find example, example can "a binary number" if define binarynumber class/struct instead of using integer make sense since if had int binarynumber1=1, binarynumber2=1; , somewhere along process binarynumber1+binarynumber2 expect result 10 instead of 2, right? define binarynumber class/struct , overload operator + - * / etc.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -