Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 26 |
Nodes: | 6 (0 / 6) |
Uptime: | 56:11:33 |
Calls: | 632 |
Files: | 1,187 |
D/L today: |
27 files (19,977K bytes) |
Messages: | 179,568 |
Compare the following:
(defun f(a)
(cons
(lambda () (setq a (1+ a)))
(lambda () (setq a (+ a a)))))
(setq g (f 2))
(funcall (car g))
(funcall (car g))
(funcall (cdr g))
(funcall (cdr g))
(setq h (f 2))
(funcall (car h))
(funcall (car h))
(funcall (cdr h))
(funcall (cdr h))
with:
class F {
public:
friend class Car {
public:
Car (F& f) : a(F.a)
{}
int operator()()
{ return a++; }
private:
int& a;
}
friend class Cdr {
public:
Cdr(F& f) : a(F.a)
{}
int operator()()
{ return a*=2; }
private:
int& a;
}
F(aa) : a(aa), car(this), cdr(this)
{}
private:
int a;
Car car;
Cdr cdr;
};
main()
{
F g(2);
F h(2);
cout << g.car() << endl;
cout << g.car() << endl;
cout << g.cdr() << endl;
cout << g.cdr() << endl;
cout << h.car() << endl;
cout << h.car() << endl;
cout << h.cdr() << endl;
cout << h.cdr() << endl;
}