September 25, 2000

Boolean Literals

The keywords false and true are Boolean literals that evaluate to 0 and 1 respectively. Note that in other programming language, VB for instance, false equals -1 and true equals 1. C++ programmers who write code that interfaces with other environments and languages such as Access are surprised to discover

Casting Pointers To Members

C++ allows casting a pointer to member functions of one type to another pointer to member, as long as the signatures of the two match. However, the results of such a cast are implementation-defined. In other words, you have to consult your compiler’s manual to make sure that it supports

Enumerator with Zero Value and Null Pointer Constants

Here’s a topic that was recently discussed at the Standardization committee. Suppose we have the following enum type: enum Stat { good, bad}; Is it legal to use the enumerator good, whose value is zero, as a null pointer constant? Put differently, is the following declaration allowed? void *p =

Hardcoded Char Arrays

Does your C++ compiler accept the following code? char s[3]=”abc”; // illegal in C++, no place for ” The standard requires that the size of a char array initialized with a literal string be sufficiently large to store a terminating null character. In other words, the declaration of s should

Violating an Exception Specification

The implementation ensures that functions having an exception specification should throw only exceptions that are listed in their exception specification. For example: class X{};class Y{};void f() throw X{ throw Y(); // violation of exception specification} By default, an attempt to throw an exception of a type that is not listed

Binding a Reference to an Rvalue

In a previous tip, , I explained the concept of lvalues and rvalues. Binding a reference to an rvalue is allowed as long as the reference is const. The rationale behind this restriction is straightforward: you can’t change an rvalue, and only a const reference ensures that the program

Extracting a Program’s Name

The name of the executable is stored in the string argv[0]. To access it, declare your main() function as follows: int main(int argc, char ** argv) Even if the application was called without any parameters from the command-line, C++ ensures that at least one argument, namely argv[0], contains a null-terminated

Leave a Grain of Debugging Info in Your Release Version

It may be tempting to squeeze an extra pint of performance by removing all debug information from the release version. However, it’s recommended to leave minimal debugging info even in the release version. On certain platforms, release versions carry by default a small baggage of “traceback” information that enables the

Helper Functions

The interface of a class consists of its public members. Usually, private members represent implementation details and are not exposed to other classes and users. Private members can be member functions, not just data members. When is it useful to declare member functions private? Suppose you have a multimedia player

No more posts to show