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
 
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.
 
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
 
Thanks for sharing this blog post, Great work.

To crack Scrum master interview: Scrum master interview questions
 
Thanks for sharing this informative content.,
Leanpitch provides online training in Scrum Master Certification , everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.

Scrum master certification online

CSM certification online
 

Thanks for sharing this informative content.,
Leanpitch provides online training in Scrum Master Certification , everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.

Scrum master training online

Scrum master certification online
 
"Thanks for your marvelous posting! I actually enjoyed reading it, you could be a great author.The jackets that we sell are replicas of the coats worn by top television celebrities because it is a growing trend to copy them when it comes to fashion. These celebrities have really become the trendsetters. This leisure was hitherto in reach of only the rich and the rest could only dream about it. But now these brown colour biker jacket for men are available for all people who want to buy them because of our affordable prices. So we welcome you to come and buy celebrity jackets of excellent quality and know more about us and our products!

"
 
Very useful post i really appreciate thanks for sharing such a nice post. I am also wanted to write blog kindly guide me if my topic is facial tissue suppliers then what should I do first and how will I create new and unique content on this topic
 
Please visit the website link & Avail pest control services today.
 
Learning is the process which I think never be ended because in every stage of life a person have able to learn new things, modify and implement to his life.
성인웹툰

 
This comment has been removed by the author.
 
wish you all the best for upcoming comments. I am also wanted to write blog kindly guide me if my topic is wholesale diamond suppliers then what should I do first and how will I create new and unique content on this topic

 
Thanks for sharing. Opal is an amorphous form of silica, chemically similar to quartz, and black opal is the most highly valued type of opal. When you see an opal crystal you will love it and I bet you will shop for black opal ring because it's really so beautiful.
 
Thomas's expertise covers a wide range of disciplines, making him well-equipped to handle Dissertation Writing Service UK from various academic fields.
 
Before applying for Illinois emergency loans with no job, you should meet several eligibility criteria set by lenders. As they are basic, almost all consumers 24 7 Payday Loans may qualify for payday cash advances.
 
The shared blog explains about the programming languages, using this easy to understand the programming language. Stafford Traffic Lawyer

 
Wow!!! What a blog. The way you explain it the way you use this word is mind-blowing. I just love this. Thanks for spreading this knowledge to us.
 
Bankruptcy lawyers in virginia
Explore professional assistance with bankruptcy matters in Virginia. Local bankruptcy lawyers can guide you through the complex legal processes, helping you make informed decisions for financial recovery.
 
In Virginia, a legal separation entails the creation of a separation agreement that addresses important matters. Couples may seek for a no-fault divorce following six months of continuous separation.
legal separation virginia

 
The style is so unique.

 
I am glad to see this site share valued information Nice one!
 
I’m hoping to check out the same high-grade blog posts.
 
Your blog posts are more interesting and impressive.
 
Post a Comment



<< Home

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