Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Partially compl...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 9499 of 9592
Post > Topic >>

Partially complete types

by Vidar Hasfjord <vattilah-groups@[EMAIL PROTECTED] > Apr 14, 2008 at 12:51 PM

In C++ you are allowed to declare an incomplete type (a "forward").
Code can operate on references and pointers to such types. This is a
very nice feature with regard to encapsulation.

I often use this feature to omit detail in interfaces; detail that not
all clients need to know. For example:

   class Component {
   //...
   public:
     class Parameter;
     virtual Parameter* get_parameter ();
   };

Client code can use Component and even pass pointers to Parameters
around while still being blissfully unaware of Parameter's definition.
Clients that need to know can include a separate header where
Parameter is defined.

This works nicely except when the Parameter class really is private
and clients only should see an abstract interface, i.e.:

   class Component {
   public:
     class IParameter {
     public:
       virtual string get_name () = 0;
       virtual string get_value () = 0;
     };
     virtual IParameter* get_parameter ();
   };

Unfortunately, now the implementation of Component and friends, helper
classes and free functions, no longer can use GetParameter without
doing a dynamic_cast should it be necessary to access Parameter
details. In other words, there is type erasure in the Component
interface.

It would be nice if it was possible to *partially* complete a type.
This would mean to specify the base (or bases) of a type without
providing the full definition. For example:

   class Component {
   public:
     class IParameter {
       //...
     };
     class Parameter : public IParameter; // partial type completion
     virtual Parameter* get_parameter ();
   };

Now the compiler can do a full type-check against what is known about
the Parameter type at any point in the code. The client code knows
only that Parameter inherits IParameter and is hence restricted to use
that interface, while the implementation code that has seen the full
Parameter definition can use all its methods and members; without need
for downcasting.

Any views on the feasability of such a feature?

Regards,
Vidar Hasfjord

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]




 6 Posts in Topic:
Partially complete types
Vidar Hasfjord <vattil  2008-04-14 12:51:07 
Re: Partially complete types
"Alf P. Steinbach&qu  2008-04-14 21:23:05 
Re: Partially complete types
Greg Herlihy <greghe@[  2008-04-14 21:26:22 
Re: Partially complete types
Marcin Swiderski <sfid  2008-04-15 23:21:48 
Re: Partially complete types
Vidar Hasfjord <vattil  2008-04-16 03:57:21 
Re: Partially complete types
Vidar Hasfjord <vattil  2008-04-16 03:58:31 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue May 13 8:07:46 CDT 2008.