Home
Fractals
Tutorials
Books
Archive
My blog
My LinkedIn Profile

BOOKS i'm reading

Cryptography engineering, Niels Ferguson, Bruce Schneier, Tadayoshi Kohno, ISBN: 9780470474242
Advanced Programming in the UNIX(R) Environment (2nd Edition), W. Richard Stevens, Stephen A. Rago, ISBN:0201433079
Trading For a Living, Alexander Elder, ISBN:0471592242

mailto:olivier@olivierlanglois.net

Archives for: July 2007, 25

07/25/07

Permalink 10:59:46 pm, by lano1106 Email , 361 words, 274 views   English (CA)
Categories: C++

forward declaration of classes in a namespace and member (nested) classes

Forward declaration is a technique that has at least 2 purposes:

  • 1- Resolve circular dependency where class A refers to class B that refers to class A.
  • 2- Reduce header file dependencies when header file A contains functions that takes pointers or references to classes defined in another header file.

For purpose #2, a TU (translation unit) that includes header file A but does not use the functions using the class defined in header file B and does not access that class, does not need to include header file B. That way if header file B ever change and forward declaration is used, this TU will not be recompiled. Taking systematically all the opportunities to reduce header file dependencies will not make a big difference on a project containing just a few TUs but it can save hours of compilation on big projects. Also, from experience, it is much easier to plan ahead and think about this aspect as the project grows than trying to remove the dependencies once they become a major problem for maintenance!

The reason why forward declarations work is because the compiler does not need to know the size of a class to generate code that handles pointer or reference to that type. A pointer or a reference always have the same size no matter the type.

Now that I have convinced you of the benefits of using forward declarations in header files, once you start using them, you might stumble on minor difficulties. How to forward declare a class inside a namespace? I have checked into the C++ bible 'The C++ Programming Language' from Bjarne Stroustrup and it remains quiet on the subject.

Fortunately, the answer is simple. Since namespace are open, all you have to do is:

namespace A
{
// Forward declaration
class B;
};

For nested classes, you can do it too but at the condition that it is done inside the outer class full declaration.

class B
{
  // Forward declaration
  class C;
};

is ok but

class B;
class B::C;

is not. If this is something that you need, you either have to give up the forward declaration of B or move out class C out of B.

Permalink 10:08:21 pm, by lano1106 Email , 112 words, 148 views   English (CA)
Categories: C++

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices, Herb Sutter, Andrei Alexandrescu, ISBN: 0321113586

I had high expectations about the fruit of the association of 2 authors that I appreciate but the result did not meet these expectations. Basically this book provides 101 rules or guidelines that you can get for free by looking at the table of content. Each of these rules is then followed by a very short explanation (1 or 2 pages usually). In my opinion, most of them are common wisdom that you can get from other sources. This is it. That is all you will get from this book. For that reason, I recommend to skip this one except if a convenient and compact collection of common knowledge is something that you are looking for.

Olivier Langlois's blog

I want you to find in this blog informations about C++ programming that I had a hard time to find in the first place on the web.

July 2007
Sun Mon Tue Wed Thu Fri Sat
 << < Current> >>
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Search

Custom Search

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 2

powered by
b2evolution