/*** book: "thinkinginc++" * function: Enum in old code hack* time: September 10, 2014 08:35:13* Author: cutter_point*/#include <iostream># Include<ctime> #include <cstdlib>using namespace Std;class bunch{ enum {size=1000}; int i[size];}; int main () { cout<< "sizeof (Bunch) =" <<sizeof (Bunch) << ", sizeof (i[1000]) =" << sizeof (int[1000]) <<endl; return 0;}
/*** book: "thinkinginc++" * Function: Comparison of const and non-const member functions * Time: September 10, 2014 08:35:36* Author: cutter_point*/#include <iostream > #include <cstdlib>//There is a random function rand header file #include<ctime>//Get system time, in order to change the seed mechanism of the random number using namespace std;/* Srand and Rand () work together to produce a sequence of pseudo-random numbers. The RAND function requires a system-supplied seed to generate a sequence of pseudo-random numbers before generating a random number, and Rand produces a series of random numbers based on the value of the seed. If the seed provided by the system does not change, the sequence of pseudo-random numbers generated by each call to the RAND function is the same. Srand (unsigned seed) alters the seed value provided by the system through the parameter seed, which allows the sequence of pseudo-random numbers generated by each call to the RAND function to be different, thus achieving a true "random" meaning. */class quoter{int lastquote;public:quoter (); int lastquote () const; Const char* quote ();}; Quoter::quoter () {lastquote=-1; Srand (Time (0)); Change the seed of the random number}int quoter::lastquote () const//This const is to make this function unable to change the member variable {return lastquote;} The const char* Quoter::quote () {static const char* quotes[]=//static Const type is: All objects are shared and unchanging data. {"Is there fun yet?", "Doctors always know Best", "Is it ... Atomic? "," Fear is obscene "," There are no scientific evidence "," to-support the idea "," that L IFE is SeriOUs "," things that do us happy, make us wise "," Cutter_point "}; const int qsize=sizeof (quotes)/sizeof (*quotes); The length int qnum=rand ()%qsize is obtained; RAND () generates random numbers according to seed while (lastquote >= 0 && qnum = = lastquote) Qnum=rand ()%qsize; Avoid successive occurrences of the same data return quotes[lastquote=qnum];} int main () {quoter q; Const Quoter CQ; Cq.lastquote (); //! Cq.quote (); This is just the return value is const, but not the const function for (int i=0; i<20; ++i) cout<<q.quote () <<endl; return 0;}
Example of "thinkinginc++" 45, a comparison of const and non-const member functions