best C++ practice for useful utilities

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

I am having fun with first time working with C++. I am converting a long owned telescope controller project where the software grew a lot of awkward tendrils over the years, so rewriting the code into a number of clean interacting objects feels quite liberating.

 

What is the best C++ approach for useful small utilities that don't really belong to some nice major system object like Telescope. Things that several big objects might use?  For example I have some functions which know how to best parse latitudes out of peculiarly encoded strings.

 

Right now I landed it in a TextUtilities object, but I thought it felt ridiculous instantiating this object to use a generically useful method. 

That's not how its done right? Where do best you locate functions like that in C++?

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Assuming your internal representation of latitudes is handled by a class, a static member function would be appropriate:

namespace My_Awesome_Project {

class Latitude {
    public:
        // assumed you are parsing from a null terminated string, not a std::string
        static Latitude parse( char const * string ) noexcept;
};

} // My_Awesome_Project

You also still have free functions in C++, but these should be placed in a custom namespace, not the global namespace (same with any classes, enums, etc you create). For things whose scope is limited to the current translation unit, use an anonymous namespace.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Meh.  Not everything has to be an Object.