Editing
C++
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Minimize includes in headers=== You should try to include the minimum number of other headers in your own header files. There are several reasons for this: * If all header files include many other headers files, which each in turn include many other header files, then compilation times can get very long. And if you change one header, you may suddenly find that practically your entire project needs to be recompiled. * By including many headers, which in turn include many other headers, you may be importing stuff you don't want and have no idea about. For example, you may be getting macro definitions that cause unexpected behaviour. This can be very hard to track to debug is there is a vast chain of headers to trace. The alternative to including the header for functionality that you want to make use of, is to forward declare the objects, and then include the header in the <tt>.cxx</tt> file. (Including headers in .cc files is more innocuous, because <tt>.cxx</tt> files aren't in turn included by other files. Here is an example. First the "before" case, where we have a certain type of header included. <source lang=c> // File "myClass.h" - example of including another header. #include "vtkImage.h" class myClass { public: myClass(); // Constructor ~myClass(); // Destructor protected: vtkImage image; // An image object in our class }; </source> Now the header file using forward declarations instead of including another header file: <source lang=c> // File "myClass.h" - example of using forward declaration. class vtkImage // Forward declaration class myClass { public: myClass(); // Constructor ~myClass(); // Destructor protected: vtkImage* image; // An image object in our class }; </source> Note that now we have a pointer instead of an actual instance of vtkImage. A forward declaration allows you to use only pointers and references to the foward-declared objects. This is because the compiler does not yet know how large the object is, but it does know how large a pointer is. Now because we have only a pointer, we need to create an instance of our image object in the cxx file, and of course get rid of it too. One way to do this is in the constructor and destructor. // File "myClass.cxx" myClass::myClass() { image = vtkImage::New(); // Create a vtkImage object } myClass::~myClass() { image->Delete(); } In this example we are using the VTK static New and Delete methods, because we have a VTK object. For other objects you will probably use the plain vanilla C++ new and delete commands.
Summary:
Please note that all contributions to bone.wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BoneWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
associated-pages
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Documents
Recent changes
Help
Tools
What links here
Related changes
Page information