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
 
Despite the low fees charged for Custom Nursing Writing Services and Online Nursing Research Paper Services, we offer high quality Custom Nursing Research Paper Services.

 
Hello, I would like to thank you for sharing this interesting information. I appreciate reading it. The material you have provided here will be useful for ma lot of people. I was actually looking for marketing plan writing services nevertheless, thanks.
 
It is important to seek medical coursework writing services and medical research paper writing services since students find help when they visit Medical Research Writing Services.

 
Nice and informative blog!!
Thanks for sharing!!
astore
 
Getting a payday loan is as easy as 5000 personal loan if you have no difficulties with meeting simple basic requirements. You can benefit from the short-term loans in case you take the 200 dollar loan direct lender to get money fast.
 
This comment has been removed by the author.
 
Wow!!!
This is really informative with your inspiration and wonderful blog post...
Thanks for visit the blog and then share it...
Btw all information visit here...
Custom Boxes Uk


 

Awesome dispatch! I am indeed getting apt to over this info, is truly neighborly my buddy.
Likewise fantastic blog here among many of the costly info you acquire. Reserve up the beneficial process you are doing here.

My web page::; 오피
(jk)

 
We often place our handbag on the kitchen counter, a bench, and some even momentarily meet the floor so for hygiene reasons alone, it is not acceptable. Latest Fashion Bags
 
Post a Comment



<< Home

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