CS253 IQ 13
Show Main.IQ13 as a slide show.
Any problems with this code?
class Times {
const int factor;
public:
Times(int f) : factor(f) { }
int operator()(int n) { return n*factor; }
};
Times f(3);
cout << f(10) << ' ' << f(12); // expect 30 36
30 36
factor
is neither public
nor private
.
factor
shouldn’t be const
, since it gets assigned.
operator()
should be const
.
- The ctor should use
auto f
instead of int f
for increased generality.
- What fine code!
What will this code produce?
const char *cap = "James T. Kirk";
auto v = find(begin(cap), end(cap), 'T');
cout << v;
c.cc:2: error: no matching function for call to 'begin(const char*&)'
true
T
T. Kirk
- some hexadecimal pointer value
- none of the above
E: end()
isn’t defined for a pointer
What will this different code produce?
const char cap[] = "James T. Kirk";
auto v = find(begin(cap), end(cap), 'T');
cout << v;
T. Kirk
true
T
T. Kirk
- some hexadecimal pointer value
- none of the above
C: v
is a const char *
. It’s a C-style string—display chars until a null characters ('\0'
) is found.
What will this code produce?
int a[] = {11,22,33,44,55,66};
auto v = find(a, a+5, 42);
cout << v;
0x7fffcac1c8b4
false
nullptr
66
- some hexadecimal pointer value
- none of the above
v
is an int *
, which gets printed as hex. Not sure if that's implementation-defined or not.