Discussing the nuts and bolts of software development

Wednesday, May 30, 2007

 

Pay it Forward: Part 1

When writing C++ code, do your compiler a favor and use forward declarations whenever possible: it will pay you back later... and more.

Forward declarations will...

AddressBook.h

//#include "Contact.h" // NO! Do this in the .cpp file instead. Unless you
// really need the definition of the class.

// Forward type declaration
class Contact; // YES!

class AddressBook
{
public:
// ...

/** Ways you can use the declaration without having the definition **/

// Member functions input parameters.
void addContact(unsigned int id, Contact const& newContact);
void addContact(unsigned int id, Contact const* newContact);

// Member functions output parameters.
void getContact(unsigned int id, Contact& contact);
void getContact(unsigned int id, Contact* contact);

// These cases are a bit more obscure than the others, but it is legal and
// still works.
void addContact(unsigned int id, Contact newContact);
Contact getContact(unsigned int id);

private:

// Member variables
Contact* m_contactPointer;
Contact& m_contactRef;

/** These some cases where the compiler needs the definition. **/

Contact::Address m_address;
static const size_t ms_contactSize = sizeof(Contact);
Contact m_contact;
Contact m_contactArray[10];

/** Here are some cases that often work with different STL implementations and
compilers, but are illegal. **/

std::vector<Contact> m_contacts;
std::auto_ptr<Contact> m_contactAutoPtr;
};
If the user of this class doesn't use the member functions that require the undefined type, he will not be required to include the header that defines it. The user will only need to do this if he uses those member functions.

Here are some rules of thumb that will help you determine whether a full definition is necessary for a certain type:
Note that a small caveat of this technique is that it sometimes sacrifices some convenience in the cases where the user will almost certainly want to interact with the member functions which require the full definition of the type. In this case it is a judgment call whether to use forward declaration or not, otherwise...

Pay it forward... and wait to be paid back.

Labels: , , ,


Comments:
I really like your posts. Being a programmer, all your tips come in handy in or another way. Yet there is more to learn. I read your posts in order to learn new things. I would definitely recommend using a c++ compiler. And also I do not know every compiler, there are many out there. And Yeah, forward declarations are useful because compiler wants to ensure no spelling mistakes or wrong passing of arguments has been made. As far as I know, the compiler first sees a declaration of classes and functions before it is used. This really helps the compiler to do better while validating the code. It also cleans up loose ends so it can produce a good looking object file. If we miss out forward declarations the compiler would produce an object file that would have to contain information about all the possible guesses as to what the function add might be. And the linker would have to work out which add you actually meant to call. It is possible that the linker may get the wrong add. Even I recommend trying it. For quick builds, it is good practice to follow. But the thing is it can take some effort and extra lines of code that may need to be maintained. I have seen many code bases where nobody bothers. It is good to put them into a separate header file if you find repeating the same forward definition. I use forward definitions in all programs. I felt that it is a good thing to do because compiler executes it faster. C++ is compiled from the top down. So it needs to know the things before they are used. Programming is really interesting. At the same time sites like yours provides good information and various tips to add while building a code.
 
Do you want to learn how to write a response paper? Here you can find some tips about it
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?