Past C/C++ 10-Minute Solutions
Despite the many instances of copy operations at your disposal, experience the efficiency of a new, emerging paradigm called move semantics.
|
Java and C# have keywords that allow you to disable inheritance, but C++ does not. Find out how you can still prevent undesirable inheritance from concrete classes and how you can disable the copying of objects that aren't meant to be copied.
|
Controlling the number of fractional digits after the decimal point is a common requirement in various application domains, but floating point variables don't support this directly. How do you still keep your floating point data neatly formatted? The answer lies in converting them to strings and formatting their textual representation.
|
Embedding a hard-coded string inside a function's body in order to retrieve that function's name can be tedious and bug-prone. Learn how to use a new C99 facility to obtain the function's name at runtime.
|
virtual member functions are widely used in object-oriented design but they don't always give you the flexibility you're after. For example, they don't let a derived class modify certain steps of a base class function while still retaining the original structure of the base class function. Learn how the Template design pattern solves this problem neatly.
|
Each built-in datatype in C++ possesses different properties, all of which contain a wealth information important to the design of your application. Find out how the library can help you programmatically access this critical data.
|
Learn how to allocate arrays of objects on a predetermined memory address using this unusual twist on placement new.
|
You can declare classes and functions friends, but the unfriendly syntax may bewilder youparticularly if you're using them in templates. Learn some simple guidelines and rules to tame friend declarations and make them do precisely what you want them to.
|
Developing financial, scientific, or numerical analysis applications? Tired of reinventing the wheel when it comes to calculating basic statistical data? Find out how you can implement statistical functions with a few useful STL algorithms.
|
C++ has come a long way, but using heterogeneous containers can still be a hassle. Find out how the Boost.Any library provides a new way to safely store objects of arbitrary datatypes in a container.
|
How do you trim a container whose capacity is larger than necessary? How do you force a container to destroy its elements and set its capacity to 0? Find out how to regulate an STL container's storage manually.
|
Learn how to simulate the implicit conversion of built-in datatypes with different specializations of the same template by adding an asymmetric assignment operator to the class template.
|
Though the changes in Visual Studio 2005 represent a major improvement over non-compliant hacks that have been in use since the mid-1990s, fear of breaking existing C++ apps has led many project managers to avoid upgrading. This checklist helps you to locate and repair incompliant code so you can upgrade your apps--without breaking them.
|
Defining an unnamed function directly on the call site of an algorithm can be very useful but is it even possible? Yes it is! Find out how using lambda expressions can simplify your use of STL algorithms.
|
Your days of being frustrated by std::auto_ptr's limitations are over! The Library Extensions Technical Report 1 has added a new smart pointer class to the standard memory header. Find out how you can use shared_ptr to automate resource management and simplify common programming tasks.
|
Changes in the implementation details of a single infrastructure class don't have to trigger a wholesale recompilation of the numerous classes referring to the modified class. Find out how applying the Pimpl idiom can help you reduce compilation time and enhance encapsulation.
|
While C++'s three memory storage types provide an unparalleled degree of flexibility with respect to memory management, this diversity can sometimes make things more difficultlike when you need to allocate objects of a certain class exclusively on the free-store. Find out how to enforce memory allocation policies by controlling the access type of a class's member functions.
|
Just because you aren't porting to 64-bit now, doesn't mean you should be ignoring the new coding guidelines. You can make a few simple adjustments now that will target your C+ code dually for 32/64-bit, leaving you in great shape for when the 64-bit requirement finally arrives.
|
Built-in arrays provide superior performance, but the trick is to use them without compromising STL compatibility. This solution will teach you to use built-in arrays properly and how to create an array whose dimensions are determined at runtime without having to resort to hefty dynamic memory allocation.
|
Because standard C++ doesn't have a library for manipulating directories, programmers are forced to use workarounds that waste their time and compromise code portability. Thankfully, you can use the quasi-standard <dirent.h> and <dir.h> libraries to manipulate directories in a portable and platform-neutral fashion.
|
The Standard Library's multimap container is very similar to the map associative containerexcept that it allows duplicate keys. Find out how this functionality makes multimap more useful than it seems.
|
While exceptions help with code reliability and data integrity, they can also complicate it. Proper cleanup is essential to making sure that your data is safe and that resources are always released. Learn how to use a local class's destructor to ensure the unconditional execution of cleanup code.
|
It's easy to associate values with an index when the index is an int, but what if you need to associate pairings of data of different types? The <map> library has an associative container that will come in handy in pairings of all data types. See how it works.
|
Tuples are fixed size collections of heterogeneous objects, and they are being added to the C++ standard. Learn how this powerful facility can help you simplify several common programming tasks.
|
Though many languages have long since disposed of their preprocessors, in C++ it is still an indispensable tool for source file management and cross-platform development. This solution shows how you can use the preprocessor to perform conditional compilation.
|
Though the base class exists to save you work, its little quirks can be tricky. What if you need to extend or alter its interface in a derived class? Or readjust a member's access? Learn how to use a using-declaration to control a member's access type and to overload member functions across a class hierarchy.
|
Inadvertent object conversion can seriously jeopardize your code's safety. Luckily, conversion operators allow you to enable or disable conversions as you wish, helping you avoid those that are ill-behaved.
|
Optimization doesn't have to be more trouble than it's worth! This month's solution shows a drawback-free technique that minimizes the amount of padding inserted between your data members, increasing speed and saving memory.
|
Tired of the manual coding required by your bulk I/O operations? Learn how using stream iterators can help you do away with this tedious choreas well as improve your apps' design and performance.
|
Using callbacks instead of direct function calls is a must in event-based applications. This usefulness also extends to other application domains, where they can really make your life easierthat is, until you need to know which pointer is bound to which function and you realize you've forgotten! This month's solution explains how you can use enum types to avoid getting lost in your array of callback pointers.
|
Binding a member function to an object or a pointer can be done with the standard set of function adapters. Learn how in this month's solution.
|
Bitwise operators are used in Digital Signal Processing, compression and encryption algorithms, GUI, and other application domains as an efficient way of manipulating strings of bits. Learn how to use them and take the pain out of the setting, clearing, and testing bit strings.
|
You know initialization is imperative for high-performing applications, but class members refuse to follow the standard rules. By learning the rulesand the exceptionsof initialization, you'll always know what methods to use and when.
|
These days, it pays to be string-savvy. Along with text processors, spellcheckers, and IDEs, new Web-oriented applications also make heavy use of strings for generating text, processing clients' requests, processing scripts, and logging. Find out how to use the specialized algorithms of class std::string to implement some common string-oriented tasks.
|
When you share data among users via a file, you put down the welcome mat for versioning problems, or worse, a security breach. Solve this problem using a simple design pattern.
|
C++ doesn't initialize automatic variables for you, and if you're unfamiliar with the right syntax or aggregate initialization, you can misuse important library functionsor you might be tempted to skip initialization altogether. Avoid creating unnecessary performance overhead, future maintenance problems, and potential bugs by learning how to correctly initialize data on your own.
|
Though the Standard Template Library offers a generic swap() algorithm, there are several other implementations from which to choose. Which implementation best suits your program's needs? This month's solution shows you how to evaluate each of them, enabling you to utilize each implementation to its greatest benefit.
|
Runtime Type Information (RTTI) was created more than a decade ago, yet most developers remain unaware of its functions and benefits. This month's solution explains when and how you can use RTTI for dynamic type detection.
|
The task of converting strings to uppercase is just like many other programming tasks: everyone needs it and everyone does it. The problem is with how they do it. Often it is done the hard wayreimplementing the wheel or introducing unnecessary performance penalties, maintenance problems, and other complexities. This month's solution demonstrates how the transform() algorithm is a faster, cleaner and more flexible method.
|
Many tasks involving networking, file management, or data compression call for programs to manipulate individual bits of memory directly. Fortunately, C++ enables you to do this without resorting to assembly programming or inefficient libraries. This solution shows you how to use fields to manipulate individual bits in a struct or a class.
|
Use exceptions to pass information about anomalous runtime conditions and to transfer control to an appropriate handler.
|
Use template specializations and partial specializations to override the default behavior of a primary template in special cases.
|
It happens oftendata in a file is lost or corrupt, and no one knows how or why. This can happen if files are being used or modified by more than one user simultaneously. Use C++ to create a lock file, ensuring that only one user at a time has access to critical files.
|
It would be a lot easier for programmers if they could simply write data to variables, letting the environment allocate storage for them. Use dynamic memory allocation and the RAII idiom.
|
Instead of relying on cin to validate input, read the input as a string and then check it. If the string passes the test, convert it to the desired type, say int or double.
|
C++ doesn't offer a direct construct to restrict object allocation to a specific storage type. How can you ensure that users don't create objects of a certain class on the stack or in static memory?
|
By default, certain signals cause a process to terminate, no questions asked. Prevent this by installing handlers for incoming signals and catch them when they occur.
|
How do you avoid the unnecessary drudgery of writing a linked list from scratch while benefiting from a generic, efficient and portable list data structure?
|
In this 10-Minute Solution, Danny Kalev shows you how to implement persistence without resorting to third-party frameworks such as DCOM and CORBA.
|
Choose the right strategies for overloading this operator for user-defined types in general.
|
The ailments of C-style cast have been known for years. C++ offers a superior alternative to it in the form of new cast operators that document the programmer's intent more clearly while preserving the compiler's ability to catch potential bugs.
|
Standard C++ provides an enhanced, object-oriented, internationalization-aware library for file I/O, namely <fstream>. This solution shows you how to use classes.
|
Instead of a built-in array, use an STL vector object to store incoming datait's safe, simple, and automatic.
|
Use stringstream objects to perform easy, safe, automatic, and object-oriented type conversions.
|
Implementing an abstract operation as an ordinary function forces you to define multiple instances of that function, thereby incurring considerable maintenance and debugging overheads. Use a function template instead of an ordinary function.
|
C provides a special mechanism for defining functions that take a variable number of arguments. These are known as variable argument list functions or variadic functions.
|
Each operating system and framework offers proprietary APIs to access the system's date and time. Learn to use the standard date and time library to retrieve and process time in a portable and efficient manner.
|
C++ Pro Danny Kalev shows you how to override the new and delete operators, first on a per-class basis, and then globally.
|
Because STL containers have value semantics i.e., they store the actual object as an element rather than storing a reference or pointer, slicing may occur when you attempt to store derived objects.
|
As you probably know, ordinary functions and member functions are two entirely different things. Even a brute-force cast won't do in this case. Danny Kalev teaches you to solve this problem.
|
Danny Kalev teaches you to read and set environment variables and support for wide character variables.
|
This month, Danny Kalev shows you how to implement a simple and effective stopwatch class that automatically calculates and reports the execution time of functions, loops, and code blocks.
|
One of the advantages of using the iostream objects is that you can customize them to support your own classes. I will show how to overload ostream's operator << so that it can display a user-defined object.
|
C++ Pro Danny Kalev discusses the fundamentals of function pointers and shows how to use them to implement callbacks. Notice that this article focuses on ordinary functions, not class member functions.
|
Danny Kalev shows how to implement the singleton pattern in C++ and explains how you can optimize its design for single-threaded applications.
|
Although pointers to functions are widely used for implementing function callbacks, C++ offers a significantly superior alternative to them, namely function objects.
|
C++ Pro Danny Kalev explores how to use this algorithm to generate random sequences of various types easily.
|
Help prevent potential memory leaks using the auto_ptr class template.
|
While many C++ programmers are familiar with the basic concepts of namespaces, in general, they tend to use namespaces passively. This Solution will show you how to declare your classes and functions in a namespace and use them in a program.
|
C++ provides several features to facilitate the task of constructing an object at a pre-determined memory position. These features include a special form of operator new called placement new, and an explicit destructor invocation. Danny Kalev demonstrates.
|
This month, Jonathan Wood expands on a previous topic and address a limitation of the code he presented at that time to extract the long version of a filename. Although my previous code worked, it did not extract the entire path.
|
Writing a DLL is pretty straightforward and there are many ways to expose DLL functionality. However, most of them involve mangling the names somewhat. Danny Kalev describes the process of exporting functions with their original names.
|
C++ Pro Jonathan Wood explains how to place icons in the system tray and have them update status and open the application.
|
C++ has a lot of things going on under the hood such as hidden arguments to methods. Pointers to members allow you to safely declare a pointer to a class method and call methods through that pointer.
|
Learn to use the PeakMessage function to see if the user has pressed a particular key during a lengthy operation.
|
With Visual C++ version 6 being released, VC++ programmers will have more complete access to the new features in COMCTL32.DLL. However, for those still using VC++ version 5, here is a quick and dirty way to add coolbars to your existing VC++ applications.
|
If you have an application that needs to change between several pointers and you don't want any flicker, C++ Pro Jonathan Wood explains a technique that worked well for him.
|
Starting with Windows 95, Microsoft added new common controls. One of the most popular is the ListView control, which can be seen in the Windows Explorer and the Windows desktop itself.
|