Function pointer oriented programming
Name:
Anonymous
2010-10-28 18:25
class ascii_counter
{
public:
ascii_counter()
{
ch = ascii_counter::next_ascii<1>;
}
char next()
{
return (this->*ch)();
}
private:
char (ascii_counter::*ch)();
template<const char C>
char next_ascii()
{
ch = ascii_counter::next_ascii<(C + 1) % 256>;
return C;
}
};
int main()
{
ascii_counter counter;
char c;
while ((c = counter.next()))
std::cout << c << std::endl;
return 0;
}
Name:
Anonymous
2010-10-28 18:31
Or you could just return a closure.
Name:
Anonymous
2010-10-28 18:35
Or you could just make your program simple and readable by not using function pointers
Name:
Anonymous
2010-10-28 23:26
Name:
Anonymous
2010-10-29 0:48
template<const char C>
I.... I don't.....
Name:
Anonymous
2010-10-29 1:19
Code like this is what gives sepples a bad name
Name:
Anonymous
2010-10-29 3:54
If this code were to work at all, it would generate an immense amount of junk code to support itself.
Here's a simple closure-based solution:
(defun next-char (c)
(code-char (1+ (char-code c))))
(define-modify-macro next-charf () next-char)
(defun make-character-counter (&optional (initial-character (code-char 0)))
(lambda () (prog1 initial-character (next-charf initial-character))))
In use:
CL-USER> (make-character-counter #\a)
#<CLOSURE (LAMBDA ()) {24D7A2B5}>
CL-USER> (funcall #<CLOSURE (LAMBDA ()) {24D7A2B5}>)
#\a
CL-USER> (funcall #<CLOSURE (LAMBDA ()) {24D7A2B5}>)
#\b
CL-USER> (funcall #<CLOSURE (LAMBDA ()) {24D7A2B5}>)
#\c
CL-USER> (funcall #<CLOSURE (LAMBDA ()) {24D7A2B5}>)
#\d
CL-USER> (make-character-counter)
#<CLOSURE (LAMBDA ()) {24DD9D15}>
CL-USER> (funcall *)
#\Nul
CL-USER> (funcall **)
#\Soh
Name:
Anonymous
2010-10-31 11:27
>>1
ch = ascii_counter::next_ascii<...
function pointer <-- character
...
I beg your pardon good sir.
Newer Posts