-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathItem2.cpp
More file actions
96 lines (72 loc) · 2.27 KB
/
Item2.cpp
File metadata and controls
96 lines (72 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Main theme of the item is that constS, enumS and inlineS must
* be preferred to #defineS as much as possible
*/
// #define PI 3.14159
/* Prefer the below declaration to the above #define. Because the #defineS:
* are not visible to the compiler,
* is not entered into the symbol table making debugging harder,
* result in multiple copies of PI wherever they are used in the program,
* cannot create a class-specific constant as it knows nothing about scope ( discussed later below )
* cannot provide any kind of encapsulation
*/
const double PI = 3.14159;
// #define "A String"
/* Its better to use constants instead of #defineS remember!!
* Two replacements for the #define above are given below:
*/
const char * const str = "A String";
//Or
#include <string>
using namespace std;
//For class specific constants, constant static member variables serve the purpose well
class Horse
{
//Intentionally left empty
};
class Stable
{
/* The constant static member 'maxHorses' donot require a definition because:
* its a constant static integral,
* we are not taking its address like '&maxHorses'
*/
static const int maxHorses = 10;
Horse h[maxHorses];
};
// Another alternative is known as the enum hack
// The enum hack is a fundamental technique of TMP ( Item 48 )
class AnotherStable
{
enum { maxHorses = 10 };
Horse h[maxHorses];
};
string anotherStr("Another String");
// Alternatives to macros like below are explored
#include <iostream>
template<typename T>
void f(const T& num)
{
cout << num;
}
// #define CALL_WITH_MAX(a,b) f((a) > (b) ? (a) : (b))
/* Drawbacks with the macro approach: ( assuming a=5 and b=0 )
* we have to remember to parenthesize all arguments in the macro body,
* CALL_WITH_MAX(++a, b); a is incremented twice,
* CALL_WITH_MAX(++a, b+10); a is incremented once,
* Hence the number of times a is incremented depends on what its being compared with,
* There is a way to escape from all this nonsense. Continue reading...
*/
/* We can get all the efficiency of a macro plus all the predictable behavior and type
* safety of a regular function by using a template for an inline function
*/
template<typename T>
void callWithMax(const T& a, const T& b)
{
f (a > b ? a : b);
}
int main()
{
int a,b;
cin >> a;
cin >> b;
callWithMax (a,b);
}