. |
Last Month's Obfuscated C++
Last month, we asked you to describe a situation in which a class declares a pure virtual function, and that function is never explicitly called, but still must be defined in order for the program to successfully compile and execute. According to the ISO/ANSI specification section 12.4, "A destructor can be declared . . . pure virtual; if any objects of that class or any derived class are created in the program, the destructor shall be defined." If any readers can come up with a legitimate reason for declaring a pure virtual destructor, please let me know! |
This Month's Obfuscated C++
This month we abuse an old friend. Although the C stdio library isn't the preferred way to do I/O in C++, it's still supported. Explain the behavior of this program:
#include
class C {
void (C::*s)();
void a(){x(a,"%c",b,"",c,""); }
void b(){x(a," %c",b,"",c,""); }
void c(){x(a,"\n%c",d,"",c,""); }
void d(){x(a,"\n %c",d,"",c,""); }
void x(void(C::*a)(),char*as,void(C::*b)(),
char*bs,void(C::*c)(),char*cs){
switch(int l=getchar()) {
case -1: s=0; return;
case ' ': case '\t': printf(bs,l); s=b; break;
case '\n': printf(cs,l); s= c; return;
default: printf(as,l); s= a; break;
}
}
public:
C():s(a){while(s)(this->*s)();}
};
int main(int,char*[]) {
C a;
return 0;
}
Rob Murray is Manager, Engineering at the Irvine office of Net Explorer, an object-oriented software consulting company based in Houston, TX. He has taught C++ at technical conferences since 1987, and is the author of C++ Strategies and Tactics. He was the founding editor of the C++ Report and can be contacted at [email protected].
|