. |
Last Month's Obfuscated C++
Last month we asked you to write a legal ISO/ANSI C++ source file that contains no preprocessor directives or comments, and contains the character sequence:
;%
That is, a semicolon immediately followed by a percent sign.
The ISO/ANSI C++ standard includes alternative tokens (also known as "digraphs"), which are synonyms for some of the normal language tokens. One of these is the digraph:
%>
which is a synonym for:
}
The following is therefore a legal C++ source file that contains the sequence we're looking for:
void f(){;%>
This is a synonym for:
void f(){;}
The ; is an empty expression statement.
You may have to explicitly enable digraphs to get your compiler to accept this file. |
This Month's Obfuscated C++
A pure virtual function can be used when there is no sensible default implementation for the virtual function. Some derived class must override the virtual function in order for objects of that type to be created:
class Base {
public:
virtual void f() = 0;
};
Base b; // Error, Base is abstract
class Derived : public Base {
public:
virtual void f() {}
};
Derived d; // Legal
No definition of Base::f is required. However, such a definition is allowed; in this case, the function may be called explicitly using the :: syntax. For instance:
d.Base::f(); // Explicit call to Base::f
This calls Base::f if it is defined; otherwise, this code will fail to link.
For this month's puzzle, describe a situation in which a class declares a pure virtual function, that function is never explicitly called, but that function must still be defined in order for the program to successfully compile and link.
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].
|