member function or global?

This is a discussion on member function or global? within the C++ Programming forums, part of the General Programming Boards category; This is an old question to which I don't remember the answer. A quick few searches didn't clarify. Suppose I ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    member function or global?

    This is an old question to which I don't remember the answer. A quick few searches didn't clarify. Suppose I have the option of building functionality into an object or having it done to the object. I recall the latter being preferable in this type of circumstance, and std::string being made a negative example. Why?
    Code:
    //minimal container of a 2-dimensional array, or whatever.
    template<class T>
    class grid
    {
      public:
          enum print_mode { text, binary }
          grid(unsigned w, unsigned h);
          T & operator()(unsigned x, unsigned y);
          const T & operator()(unsigned x, unsigned y) const;
          const unsigned width() const;
          const unsigned height() const;
          //should it be:
          void print_to_file(const std::string & path, print_mode m);
          ~grid();
      protected:
          //whatever
    };
    
    //or should it be:
    template<class T>
    void print_to_file(const & grid<T> g, const std::string & path, grid<T>::print_mode m);
    
    //I suppose the second method would move enum print_mode outside of grid, too.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,171
    O_o

    Generally speaking, a class should be able to serialize itself when handed a standard stream or block device. You aren't doing that. You have a function that opens a device and constructs an apparently design specific representation. If this function can be created as a non-member, non-friend function, it should be created as a non-member, non-friend function.

    "Monoliths" are considered "bad" because they usually lack simplicity thanks to a bloated interface and multiple responsibilities.

    If you look at the `std::string' class, you'll find dozens of similar functions. Instead of focusing on a few methods that provide a complete interface, the class has methods that do nothing more than provide an "ease of use" interface. (I'm not talking about the various operators.) The problem is, the class has so many "ease of use" methods that the interface is difficult to remember and lacks consistency. The few methods designed around the iterators pattern are sufficient.

    Soma

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    You have a function that opens a device and constructs an apparently design specific representation. If this function can be created as a non-member, non-friend function, it should be created as a non-member, non-friend function.
    Instead of focusing on a few methods that provide a complete interface, the class has methods that do nothing more than provide an "ease of use" interface.
    Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21