Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

382 Part VI: The Part of Tens

find when the function is examined by itself rather than after it has been thrown into the pot with the rest of the functions — and your attention has gone on to new programming challenges.

Avoid Overloading Operators

Other than using the assignment operator operator=(), you should hold off overloading operators until you feel comfortable with C++. Overloading oper­ ators other than assignment is almost never necessary and can significantly add to your debugging woes as a new programmer. You can get the same effect by defining and using the proper public member functions instead.

After you’ve been C-plus-plussing for a few months, feel free to return and start overloading operators to your heart’s content.

Heap Handling

As a general rule, programmers should allocate and release heap memory at the same “level.” If a member function MyClass::create() allocates a block of heap memory and returns it to the caller, there should be a member func­ tion MyClass::release() that returns the memory to the heap. Specifically, MyClass::create() should not require the parent function to release the memory. This certainly doesn’t avoid all memory problems — the parent function may forget to call MyClass::release() — but it does reduce the possibility somewhat.

Using Exceptions to Handle Errors

The exception mechanism in C++ is designed to handle errors conveniently and efficiently. In general, you should throw an error indicator rather than return an error flag. The resulting code is easier to write, read, and maintain. Besides, other programmers have come to expect it — you wouldn’t want to disappoint them, would you?

It is not necessary to throw an exception from a function that returns a “didn’t work” indicator if this is a part of everyday life for that function. Consider a function lcd() that returns the least common denominators of a number passed to it as an argument. That function will not return any values when presented a prime number (a prime number cannot be evenly divided by any other number). This is not an error — the lcd() function has nothing to say when given a prime.

Chapter 29: Ten Ways to Avoid Adding Bugs to Your Program 383

Avoiding Multiple Inheritance

Multiple inheritance, like operator overloading, adds another level of com­ plexity that you don’t need to deal with when you’re just starting out. Fortunately, most real-world relationships can be described with single inher­ itance. (Some people claim that multiple inheritance is not necessary at all — I’m not sure that I’m not one of them.)

Feel free to use multiple-inherited classes from commercial libraries. For example, the Microsoft MFC classes that are key to Visual Studio 6 make heavy use of multiple inheritance. Microsoft has spent a considerable amount of time setting up its classes, and it knows what it’s doing.

After you feel comfortable with your level of understanding of C++, experi­ ment with setting up some multiple inheritance hierarchies. That way, you’ll be ready when the unusual situation that requires multiple inheritance to describe it accurately arises.

384 Part VI: The Part of Tens

Chapter 30

The Ten Most Important Optional

Features of Dev-C++

In This Chapter

Customize editor general settings to your taste

Highlight matching braces and parentheses

Enable exception handling

Include debugging information (sometimes)

Create a project file

Customize the help menu

Reset breakpoints after editing a file

Avoid illegal filenames

Include #include files in your project

Executing the profiler

This chapter reviews some of the settings within the Dev-C++ environment that might affect you on a normal day of C++ programming. This chapter

also touches on the Dev-C++ profiler.

Customize Editor Settings to Your Taste

Programming should be a pleasant experience. C++ has enough unpleasant things to deal with, so you don’t need an editor that doesn’t think like you do. Fortunately, Dev-C++ allows you to “have it your way.” Choose Tools Editor Options to change editor settings.

Let me start with a few settings that don’t make much difference. For example, I prefer four spaces for a tab — you might prefer another amount. In addition, I have the editor draw a line down column 60 on the display to keep a single line of code from extending so far that I can’t see the rest of my program.