Ryan's District
November 22, 2008, 03:43:39 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Ryan's District Lottery: Claim your ticket or check
Jackpot details  
 
   Home   Help Search Chat Calendar Chess Shop Login Register  
Digg This!
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: C++ tips - Minimize compilation dependencies between files  (Read 3584 times)
0 Members and 1 Guest are viewing this topic.
ben2ong2
Quality Poster
Paid
Hero Member
*****

Reputation: 17
Offline Offline

Gender: Male
Posts: 2374
9976.80 RD$

View Inventory
Send Money to ben2ong2

View Profile Awards
« on: October 07, 2006, 08:48:48 AM »

[ This material adapted from "Effective C++" by Scott Meyers,
  page 111. -adc ]

"C++ doesn't do a very good job of separating interfaces from
implementations. In particular, class declarations include not
only the interface specification (typically in the public and
protected sections), but also the implementation details. For
example:

   class Person {
     private:
      TString  fName;
      TDate    fBirthdate;
      TAddress fAddress;
      TCountry fCountry;
     public:
      Person (const TString& name, const TDate& date,
              const TAddress& a, const TCountry& c);
      [...]
   };

[...] the class Person can't be compiled unless the compiler also
has access to declarations for the classes in terms of which
Person is implemented, namely, String, Date, Address, and Country.

[...] Unfortunately this sets up a compilation dependency between
the file containing the Person class declaration and these additional
files.

[...] Here is how you employ the technique to decouple the Person's
interface from it's implementation. [...]

   class TString;
   class TDate;
   class TAddress;
   class TCountry;

   class TPersonImpl;

   class Person {
     private:
      TPersonImpl* fImpl;
     public:
      Person (const TString& name, const TDate& date,
              const TAddress& a, const TCountry& c);
      [...]
   };

Now clients of Person are completely divorced from the details of strings,
dates, addresses, countries and persons. Those classes can be modified
at will, but clients of Person may remain blissfully unaware. More to the
point, they may remain blissfully un-recompiled. This is true separation
of interface and implementation.

A different approach is to make Person an Abstract Base Class (ABC) [...].
It typically has no data members, no constructors, a virtual destructor,
and a set of pure virtual functions that specify the interface. An ABC for
Person might look like this:

   class Person {
     public:
      virtual ~Person ();

      virtual const char* name ()        const = 0;
      virtual const char* birthdate ()   const = 0;
      virtual const char* address ()     const = 0;
      virtual const char* nationality () const = 0;
   };

Clients of the Person class program in terms of Person pointers and
references, since it is not possible to instantiate classes containing
pure virtual functions.

[...] "What does all this hocus-pocus cost me?," you mutter. The answer
is the usual one in computer science: it costs you some speed at runtime,
plus some additional memory per object.

Logged

You are not allowed to view links.
Register or Login
Free Paid Survey & Home Business Resources.

You are not allowed to view links.
Register or Login
Free Article Directory | Quality Content
Ryan's District
« on: October 07, 2006, 08:48:48 AM »

 Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Archive - WAP2 - WAP - imode
Sponsors: RAYAN.tv
-

Powered by MySQL Powered by PHP Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 2.856 seconds with 30 queries.

Google visited last this page Yesterday at 01:22:14 PM