member function design is the design of 'good' member functions.
- Follow a good function design. Note: many points of advice!
- Prefer writing short member functions [3] (and helper functions [18-20]).
- Attempt to make your names so clear that comments are unnecessary [24]
Member function or friend/non-friend helper function?
- Prefer non-friend helper functions to member functions [4,5]
- Make >> and
operator<<
a befriended helper function [1] - Make a function that needs type conversions on its leftmost argument, a befriended helper function [1]
- Make functions that can be implemented using the (public) interface a helper functions [1]
- Make a function a member function only if it needs direct access to the representation of a class, else make it a helper function [10]
- The operators = () [] and -> must be member functions [1]
- Use helper functions for symmetric operators [21], for example operator+ and operator-
- Use member functions to express operators that require an lvalue as their left-hand operand [22], for example operator+=
- Design constructors, assignments, and the destructor as a matched set of operations [13]
- Define a constructor to handle initialization of objects [6]
- By default declare single-argument constructors explicit [7] Make constructors explicit whenever possible [8]
- If a class is a container, give it an initializer-list constructor [15]
- If a class has a reference member variable, it probably needs a copy constructor and copy assignment operator)
Regular member functions
- Use function overloading and default arguments to create an intuitive, easy-to-use interface [24]
- Provide setters and getters for a member variable only if the fundamental semantics of a class requires them [23]
- Avoid duplication in const and non-const member functions [2]
- Make functions that should behave virtually a virtual member functions [1]
- If a class has a virtual member functions, it needs a virtual destructor [14]
- Declare a member function that does not modify the state of its object a const member function [9,11]
- Make a function that needs access to the representation of a class but needn't be called for a specific object a static member function [12]
- Herb Sutter. Exceptional C++. ISBN: 0-201-61562-2. Item 20: Class mechanics.
- Scott Meyers. Effective C++ (3rd edition). ISBN:0-321-33487-6. Item 3, paragraph 'Avoid duplication in const and non-const member functions'.
- Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. AV Rule 1: 'Any one function (or method) will contain no more than 200 logical source lines of code.'
- Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 23: Prefer non-member non-friend functions to member functions.
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 44: 'Prefer writing nonmember nonfriend functions'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 16.4. Advice. page 479: '[4] Define a constructor to handle initialization of objects'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 16.4. Advice. page 479: '[5] By default declare single-argument constructors explicit'
- Herb Sutter. Exceptional C++. ISBN: 0-201-61562-2. Item 20, page 71, top guideline :'Watch out for hidden temporaries created by implicit conversions. One good way to avoid this is to make constructors explicit when possible, and avoiding writing conversion operators'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 16.4. Advice. page 479: '[6] Declare a member function that does not modify the state of its object const'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 16.4. Advice. page 479: '[8] Make a function a member only if it needs direct access to the representation of a class'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 16.4. Advice. page 479: '[10] Make a member function that doesn't modify the value of an object a const member function'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 16.4. Advice. page 479: '[11] Make a function that needs access to the representation of a class but needn't be called for a specific object a static member function'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[1] Design constructors, assignments, and the destructor as a matched set of operations'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[4] If a class has a virtual function, it needs a virtual destructor'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[8] If a class is a container, give it an initializer-list constructor'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[10] If a class has a reference member, it probably needs copy operations (copy constructor and copy assignment)'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 18.5. Advice, page 547: '[2] Redefine or prohib '
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 20: 'Avoid long functions. Avoid deep nesting'
- Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. AV Rule 1: 'Any one function (or method) will contain no more than 200 logical source lines of code.'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 12.7. Advice. page 341: '[3] Keep functions short'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 18.5. Advice. page 548: '[8] Use nonmember functions for symmetric operators'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 18.5. Advice. page 548: '[9] Use member functions to express operators that require an lvalue as their left-hand operand'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 18.5. Advice. page 548: '[11] Provide "set() and get() functions" for a data member only if the fundamental semantics of a class requires them'
- Bruce Eckel. Thinking in C++, second edition, volume 1. 2000. ISBN: 0-13-979809-9. Chapter B: Programming Guidelines. Item 7: 'When you create a class, make your names as clear as possible. Your goal should be to make the client programmer’s interface conceptually simple. Attempt to make your names so clear that comments are unnecessary. To this end, use function overloading and default arguments to create an intuitive, easy-to-use interface.'