Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Best design for code used in many classes

Best design for code used in many classes

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Weichao Wang
    wrote on last edited by Weichao Wang
    #1

    Dear all,
    In a project I've defined many a class, each in its own header and source file. I'd like to define still a few functions, which do not belong to any of the classes, but might be used in all classes (for example, we can imagine that functions as max(), min(), swap() etc could be used in many situations). I think it a stupid methode to define these functions in all classes, not to mention that so many "copy and paste" is not in line with object oriented design / code reuse. What's the best design to realize this?
    Weichao

    Gojir4G 1 Reply Last reply
    0
    • W Weichao Wang

      Dear all,
      In a project I've defined many a class, each in its own header and source file. I'd like to define still a few functions, which do not belong to any of the classes, but might be used in all classes (for example, we can imagine that functions as max(), min(), swap() etc could be used in many situations). I think it a stupid methode to define these functions in all classes, not to mention that so many "copy and paste" is not in line with object oriented design / code reuse. What's the best design to realize this?
      Weichao

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      @Weichao-Wang Hi,
      You have several possibilities. You can define static function in a h file, you can wrap these functions in a class or a namespace, or let them global in the h file

      Here is an example using a class

      //tools.h
      class Tools{
          static int max(int a, int b){
              return a > b ? a : b;
          }
      };
      
      //Call from another file in the project... 
      #include "tools.h"
      ...
      int test = Tools.max(1, 2);
      

      or without class:

      //tools.h
      static int max(int a, int b){
          return a > b ? a : b;
      }
      
      //Call from another file in the project... 
      #include "tools.h"
      ...
      int test = max(1, 2);
      
      W 1 Reply Last reply
      3
      • Gojir4G Gojir4

        @Weichao-Wang Hi,
        You have several possibilities. You can define static function in a h file, you can wrap these functions in a class or a namespace, or let them global in the h file

        Here is an example using a class

        //tools.h
        class Tools{
            static int max(int a, int b){
                return a > b ? a : b;
            }
        };
        
        //Call from another file in the project... 
        #include "tools.h"
        ...
        int test = Tools.max(1, 2);
        

        or without class:

        //tools.h
        static int max(int a, int b){
            return a > b ? a : b;
        }
        
        //Call from another file in the project... 
        #include "tools.h"
        ...
        int test = max(1, 2);
        
        W Offline
        W Offline
        Weichao Wang
        wrote on last edited by
        #3

        @Gojir4
        Many thanks!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved