Click to See Complete Forum and Search --> : Global functions vs Member functions
Shiney Vang
February 4th, 2003, 11:28 AM
I have a newbie C++ dilemma. When would it be advantageous to encapsulate a function into a class? And when would it be better to have that function by itself (i.e. not associated with any class)?
For example, think about sorting a list. Would it be better to have the sort function as a member function of the type LIST? Or create the sort function as a separate function from the type LIST? This is just an example. In general, what is the rule of thumb?
Thanks in advance.
galathaea
February 4th, 2003, 11:54 AM
Originally posted by Shiney Vang
I have a newbie C++ dilemma. When would it be advantageous to encapsulate a function into a class? And when would it be better to have that function by itself (i.e. not associated with any class)?
Well... There are 2 different things c++ programmers refer to as "classes". The most common is the object you define with the keyword class. But there is some momentum in the community to define a class as the collection of all functions that refer to a particular object type (along with all of that object type's state) as the class of the object, whether or not the method is a member of the object type or not.
So basically, the question you ask is not one with a well-accepted answer, and there are many rules of thumb either way. Its more of a religious answer than anything. Several operator functions are commonly not placed as members, for example. However, all of that said, there are some commonly accepted rules of thumb for the particular question you ask. Since your function deals specifically with your object type (and presumably no others), it seems most natural to place it in the object type. This will keep that code encapsulated in the translation unit for the object type and be a more natural place to access that method. It also makes it easy to find the method to work on it if changes must be made in the future.
Global methods are in general not to be considered except in circumstances where it brings more flexibility to the use of the method. Examples are found in the algorithms in STL, where the algorithms are parametrized to deal generally with all container types that expose a particular interface (even your own). In these circumstances, it is common to use global methods (suitably enclosed in a namespace to prevent global name leakage).
Originally posted by Shiney Vang
For example, think about sorting a list. Would it be better to have the sort function as a member function of the type LIST? Or create the sort function as a separate function from the type LIST?
Now, with that said, I would also suggest checking into STL for the particular case you mention (if it is not purely for example). The standard containers allow sorting through various means and are general enough to allow containment of any data structures or other object types you may have in mind.
kuphryn
February 4th, 2003, 02:19 PM
A class comprises of data and controls for an object. If you can describe a set of functions and data as one object, then incorporate them into a class.
Kuphryn
slider
February 6th, 2003, 06:17 AM
You shouldn't think what to use global function or member-function.
The point is class is an object, in common. So if you see an object in your task (probrem to be solved) you should create a class. Otherwise use a global function.
You shouldn't think about member-functions as functions. Just imaging that member-function is an action that could bee done by an object.
So if you are writting a List class it is preferable to use member-func to compare two lists 'cause comparing is a list action (operation). If you are using class written by someone else it is better to use global function (there is no need to enherite your own class for one operation).
Graham
February 6th, 2003, 10:24 AM
Originally posted by slider
You shouldn't think what to use global function or member-function.
The point is class is an object, in common. So if you see an object in your task (probrem to be solved) you should create a class. Otherwise use a global function.
You shouldn't think about member-functions as functions. Just imaging that member-function is an action that could bee done by an object.
So if you are writting a List class it is preferable to use member-func to compare two lists 'cause comparing is a list action (operation). If you are using class written by someone else it is better to use global function (there is no need to enherite your own class for one operation).
No. std::list implements many functions as members because of the pecuiarities of a list mean that the standard algorithms do not work. vector and deque do not have member functions for comparison, sorting, etc. because the standard algorithms work perfectly well with them.
There is a compelling argument that says that if you can perform an operation using only the public interface of a class, then you should supply it as a free function alongside that class - not as a member function. This argument does suffer, though, from potential confusion about which functions are members: object.function(); and which are not: function(object);.
By the way we're talking about free functions, not global functions. Global implies that the function is not part of any namespace - a free function is simply one that is not a member of a class (but may be in a namespace). All the STl algorithms are free functions, but they're not global. (Just being a bit pedantic, there. :-) )
slider
February 7th, 2003, 02:02 AM
If you're talking about STL it uses free functions to avoid multiple implementation of the same actions. It is possible to do so because all kinds of actions on STL classes (lists, stacks, etc.) are made through iterators. Iterators provide the same interface for all data types supplied by STL.
So you can see that free functions in STL simplify the implementation of classes.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.