devxlogo

Tip Bank

Passing unmodifiable arguments as constobjects

Passing large objects by value as function arguments is very inefficient. On the other hand, passing them by reference/address is dangerous since it enables the callee to change its arguments

Prefer References Over Pointers

Even experienced C++ programmers who have prior experience with C tend to use pointers excessively whereas references may be a better choice. Pointers may result in bugs like the following:

Copy constructor and operator= go together

If you do not define a copy constructor and operator = , the compiler will create them automatically for you. However, there are cases when you have to define them

Better alternative to #define constants

Using #define to create macro constants is not ideal. Macros are substituted by the preprocessor, so the compiler usually cannot detect anomalies such as type mismatch (see example); furthermore, most

Explicit destructor invocation

One of the good things about destructors is that they are called automatically. There are rare cases, however, when you want to invoke an object

Make the Right Comparison

Although Java’s String package is a lot more friendly than C’s string handling, you can still find yourself surprised by its behavior at times. Consider this code: import java.lang.String;public class

What are we Looking at?

To present your applet in a visually pleasing way, it’s often handy to know the size of the screen that’s available for output. Fortunately, a built-in AWT function makes determining

A const member function

Whenever you define a class member function which does not (and should not) change its object, it’s a good idea to define it as a const member function. There are