a + b
is the same value as b + a
.
for (i = 0; i > 0; i++)
doesn't do anything.
strcpy(l, r)
behaves like l = r
.
cls c; cout << "Number of calls = " << c.call_count << ".\n"; c.f(); cout << "Number of calls = " << c.call_count << ".\n"; c.call_count++;
class cls { public: const unsigned & call_count; cls() : call_count(_call_count), _call_count(0) { } void f(void) { _call_count++; } private: unsigned _call_count; };Casting doesn't work.
And this one doesn't:int main(void) { cls c; cout << "call count = " << c.call_count << ".\n"; c.f(); cout << "call count = " << c.call_count << ".\n"; return EXIT_SUCCESS; } $ g++ -o c c.cc $ ./c call count = 0. call count = 1. $
int main(void) { cls c; cout << "call count = " << c.call_count << ".\n"; c.call_count++; cout << "call count = " << c.call_count << ".\n"; return EXIT_SUCCESS; } $ g++ -o c c.cc c.cc: In function int main(): c.cc:26: increment of read-only location $
const
mean?
const unsigned cc = c.call_count
mean?
call_count
?
const
and read-only are two different things; don't confuse them.
class cls { public: cls() : call_count(0) { } void f(void) { callcount++; } unsigned call_count(void) const { return callcount; } private: unsigned callcount; };
()
.
c.call_count
vs c.call_count()
?
#include "bogy.h" #define loop_maximum 10000000 class cls { public: cls() : call_count(_call_count), _call_count(0) { } const unsigned & call_count; unsigned callcount(void) const { return _call_count; } void f(void) { _call_count++; } private: unsigned _call_count; }; int main(int argc, char * argv[]) { cls c; time_t s, oh; timeit(oh, bogy(0)); printf("Loop overhead for %d iterations is %5.3f usec/iteration.\n", loop_maximum, ((double) oh)/((double) loop_maximum)); timeit(s, bogy(c.call_count);); printf("Time per call for %d calls: %5.3f usec.\n", loop_maximum, ((double) (s - oh))/((double) loop_maximum)); timeit(s, bogy(c.callcount());); printf("Time per call for %d calls: %5.3f usec.\n", loop_maximum, ((double) (s - oh))/((double) loop_maximum)); return EXIT_SUCCESS; }
$ uname -a SunOS clayton 5.7 Generic_106541-11 sun4u sparc SUNW,Ultra-5_10 $ g++ -o cct cct.cc bogy.cc $ cct Loop overhead for 10000000 iterations is 0.057 usec/iteration. Time per call for 10000000 calls: 0.017 usec. Time per call for 10000000 calls: 0.036 usec. $ g++ -O3 -o cct cct.cc bogy.cc $ cct Loop overhead for 10000000 iterations is 0.029 usec/iteration. Time per call for 10000000 calls: -0.017 usec. Time per call for 10000000 calls: 0.001 usec.
$ CC -o cct cct.cc bogy.cc cct.cc: bogy.cc: $ ./cct Loop overhead for 10000000 iterations is 0.034 usec/iteration. Time per call for 10000000 calls: 0.002 usec. Time per call for 10000000 calls: 0.016 usec. $ CC -fast -o cct cct.cc bogy.cc CC: Warning: -xarch=v8plusa is not portable cct.cc: bogy.cc: CC: Warning: -xarch=v8plusa is not portable $ ./cct Loop overhead for 10000000 iterations is 0.009 usec/iteration. Time per call for 10000000 calls: 0.006 usec. Time per call for 10000000 calls: 0.000 usec.
const
, otherwise, don't.
This page last modified on 22 March 2001.