Changeset 1141:e5ee2726abe4 in lemon-0.x for doc
- Timestamp:
- 02/07/05 18:35:25 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1540
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/named-param.dox
r955 r1141 5 5 \section named-func-param Named "Function" Parameters 6 6 7 C++ makes it possible to use default parameter values when calling a function. In such 8 a case we do not have to give value for parameters, the program will use the default ones. 9 Unfortunately sometimes this is not enough. If we do not want to give values for all the parameters, only 10 for some of them we come across problems, because an arbitrary set of parameters cannot be omitted. On the other hand parameters have a fixed order in the head of the function. 11 C++ can apply the default values only in the back of the order, if we do not give other value for them. 12 So we can not give the function for example the value of the first, and the third parameter, expecting that the program will aplly the default value for the second parameter. 13 However sometimes we would like to use some functinos exactly in this way. With a crafty trick and with some little inconvenience this is possible. We have implemented this little trick as an example below. 14 15 \code 16 class named_fn 17 { 18 int _id; 19 double _val; 20 int _dim; 21 22 public: 23 named_fn() : _id(0), _val(1), _dim(2) {} 24 named_fn& id(int p) { _id = p ; return *this; } 25 named_fn& val(double p) { _val = p ; return *this; } 26 named_fn& dim(int p) { _dim = p ; return *this; } 27 28 run() { 29 printf("Here is the function itself."); 30 } 31 }; 32 \endcode 33 34 35 The usage is the following. 36 37 We have to define a class, let's call it named_fn. 38 Let us assume that we would like to use a parameter, called X. In the named_fn class we have to define an _X attribute, and an X function. The function expects a parameter with the type of _X, and sets the value of _X. After setting the value the function returns the class itself. The class also have to have a function, called for example run(), we have to implement here the original function itself. The constructor of the class have to give all the attributes like _X the default values of them. 39 40 If we instantiate this class, the default values will be set for the attributes (originally the parameters), initially. If we call the X function, we get a class with the modified parameter value of X. Therefore we can modify any parameter-value, independent from the order. To run the algorithm we have to call the run() function at the end of the row. 41 42 Example: named_fn().id(3).val(2).run(); 7 43 8 44 \section traits-classes Traits Classes 9 45 46 The procedure above can also be applied when defining classes. In this case the type of the attributes can be changed. 47 Initially we have to define a class with the default attribute types. This is the se called Traits Class. Later on 48 the types of these attributes can be changed, as described below. In our software \ref DijkstraDefaultTraits is an example of how a traits class look like. 49 10 50 \section named-templ-param Named Class Template Parameters 11 51 12 I nstead of creating a new traits class you can also use this adaptor class13 like this 52 If we would like to change the type of an attribute in a class that was instantiated by using a traits class as a template parameter, and the class contains named parameters, we do not have to reinstantiate the class with new traits class. Instead of that adaptor classes can be used like in the following cases. 53 14 54 \code 15 55 Dijkstra<>::SetPredNodeMap<NullMap<Node,Node> > 16 56 \endcode 57 17 58 It can also be used in conjunction with other named template 18 59 parameters in arbitrary order. 60 19 61 \code 20 62 Dijkstra<>::SetDistMap<MyMap>::SetPredMap<NullMap<Node,Edge> > 21 63 \endcode 22 64 65 The result will be an instantiated Dijkstra class, in which the DistMap and the PredMap is modified. 66 23 67 \section named-templ-func-param Named "Function" Template Parameters 24 68 69 If the class has so called wizard functions, the new class with the modified tpye of attributes can be returned 70 by the appropriate wizard function. The usage of these wizard functions is the following: 25 71 26 72 */
Note: See TracChangeset
for help on using the changeset viewer.