Thread: design consideration

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    design consideration

    Hello

    When you have particular function (which is just one) in more classes that does the same thing with some specific element (member) what approach do you take to solve that problem?

    For instance if I have 2 classes with the same function (that does the same thing with somedata object):

    Code:
    class first_class {
    public:
      void some_function();
     
    public:
      datatype somedata;
    };
    
    class second_class {
    public:
      void some_function();
     
    public:
      datatype somedata;
    };
    What approach would you use in such cases to profesionalize and minimize the code?

    Should I make new class with this function and derive from it?
    Or would it be better to make global function that accepts somedata as a reference?
    Is there any better approach?

    Many thanks in advance!

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It depends on what some_function() does....

    Example 1:
    Assuming some_function() does the same exact thing.
    Code:
    class base
    {
    public:
      void some_function()
      {
        setScreenResolution(1024, 768);
      }
    };
    
    class first_class : public base
    {
    private:
      datatype somedata;
    };
    
    class second_class : public base
    {
    private:
      datatype somedata;
    };
    Example 2:
    Assumes that somdata is altered in a uniform way. And presumes some_function() to act identically in both classes.
    Code:
    template <typename datatype>
    class my_template
    {
    public:
      void some_function()
      {
         somedata += 55;
      }
    
    private:
      datatype somedata;
    };
    Example 3:
    Assumes nothing about some_function() other than it must exist.
    Code:
    class abc
    {
    public:
      virtual void some_function() = 0;
    };
    
    class first_class : public abc
    {
    public:
      virtual void some_function() 
      {
        std::cout << "foo";
      }
    
    private:
      datatype somedata;
    };
    
    class second_class : public abc
    {
    public:
      virtual void some_function() 
      {
        std::cout << "bar";
      }
    
    private:
      datatype somedata;
    };
    Example 4:
    This one assumes nothing about what some_function() does--rather guarantees it must exist, it only makes a defined type, somedata.
    Code:
    template <typename datatype>
    class my_abc_template
    {
    public:
      virtual void some_function() = 0;
    
    private:
      datatype somedata;
    };

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    For what you are describing, I would imagine Example 2 would probably be sufficient.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Perhaps some_function should be a member of the datatype class. If datatype is not a class (or perhaps even if it is), then make some_function a non-member function.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I was almost of the impression that datatype was a basic datatype. Though I could be mistaken. So yeah... If datatype is a class, take Daved's sage words into consideration. If it is a basic type, then take your pick of which situation suits your needs from my examples.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If it is a basic type, then take your pick of which situation suits your needs. <<

    The more I think about it, the more a non-member function seems to make sense in all cases.

    For example, vector<T> and deque<T> could both have had a some_function called sort that worked on the member data where datatype is a random access iterator. Instead of making duplicate member functions in the two classes, sort was made to be a non-member function that took somedata as two random access iterator parameters to the range to sort.

    This is basically true of all the algorithms in <algorithm>. They could have been members of the various container classes, but there would be a lot of duplicate code. Instead they were generalized and made to be non-member functions. In this case, since datatype is the same, that non-member function doesn't have to be templatized, and inheritance doesn't appear to be necessary at all.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not arguing with you. But he could be making a container. You saw his example... I am grasping at straws here. Plus I answered his question in all possible forms of his implementation. I recommended a template over some of the other options that did involve inheritance.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I wasn't correcting you, but rather giving more information to the OP after I had thought about it a little more. I was just using your quote for context.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sorry.. I need to tone down the defensiveness. You know how it can be with some of the others.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    datatype is a map in my case and datatypes in both classes are the same.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well go forth and do as you have been taught. Make Daved proud.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is non-member function meant to be global function that takes a reference of datatype as an argument?

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yes. What Daved was specifically saying there is only make it a non-member function (just a global function or even a static function) if you are not dealing with a class OR if you are dealing with a class which is more or less "finalized" and you don't have much say in how it changes (i.e. the STL templates--which sure you *could* change, but you'd just be making a non-standard implementation).

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    630
    Do you think its better to have static class functions or global in such cases?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think a global function in an appropriate namespace is generally better unless the code is very specific to the class you are making it a static member of. It doesn't sound like it is that specific in this case (and since datatype is a map you can't make it a static member of map anyway), so a global function would be appropriate.

    >> Is non-member function meant to be global function that takes a reference of datatype as an argument?

    Yes.

    Consider typedef'ing the map or taking templated iterators (like the standard algorithms) for your global function. If you don't expect you'll ever use this function again because it is pretty specific, then doing so would be overkill. However, if the contents of the function are pretty generic then you might consider making the interface generic, too. Or you could just wait until you need that functionality for a different datatype and generalize it then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. design consideration
    By l2u in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2008, 03:11 PM
  2. any comments about a cache design?
    By George2 in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 12:53 PM
  3. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 04:40 PM
  4. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM