The Artima Developer Community
Sponsored Link

Weblogs Forum
C++ Delegation and Dynamic Inheritance

5 replies on 1 page. Most recent reply: Jan 26, 2007 12:25 PM by Jim Bish

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

C++ Delegation and Dynamic Inheritance (View in Weblogs)
Posted: Aug 17, 2005 5:14 AM
Reply to this message Reply
Summary
Automating the delegation of implementation of an interface to an expression.
Advertisement
For some time I have looked for a way to have automated delegation and dynamic inheritance in C++ without introducing extra pointers in a class.

Delegation is a technique of forwarding the implementation of a particular interface to a field or expression. Typically in C++ this is only done manually, i.e.:

  class MyClass {
    FuBar fb;
    void Fu() { fb.Fu(); }
    void Bar() { bar.Bar(); }
  }

The problem with this is that it is horribly tedious, and tedium always screams at me "there has got to be a better way". Well I have found a way to automate delegations, without introducing extra pointers into a class., I've described it briefly in this article at CodeProject.

For those casually interested, I did this so I can reduce the work performed by HeronFront the Heron to C++ translator. This technique lets me do some really cool stuff, without having to introduce type-checking and template expansion into the translator.


Harrison Ainsworth

Posts: 57
Nickname: hxa7241
Registered: Apr, 2005

Re: C++ Delegation and Dynamic Inheritance Posted: Aug 17, 2005 6:15 AM
Reply to this message Reply
aren't the ToDerived<Delegator> clauses in the delegation interfaces missing a template parameter?

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: C++ Delegation and Dynamic Inheritance Posted: Aug 17, 2005 6:18 AM
Reply to this message Reply
> aren't the ToDerived<Delegator> clauses in the delegation
> interfaces missing a template parameter?

The second parameter it is automatically deduced by the compiler, because it is passed as a function argument.

Harrison Ainsworth

Posts: 57
Nickname: hxa7241
Registered: Apr, 2005

Re: C++ Delegation and Dynamic Inheritance Posted: Aug 18, 2005 6:10 AM
Reply to this message Reply
> The second parameter it is automatically deduced by the compiler, because it is passed as a function argument.

aha, yes.
(i know the details of templates almost as badly as some the compilers. (maybe that's a good thing...))

now i am not so clear about the exact motivation/problem.

the basic case of delegation requires writing, eg.:
void method1() { pObject_m->method1() }
-- which is simpler than what you have. so you must be aiming at something else.

and it seems it is still necessary to write the ___Delegation structs and their methods (at least the signature parts).

so i feel there is some larger generalisation/problem not explained.

?

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: C++ Delegation and Dynamic Inheritance Posted: Aug 18, 2005 6:31 AM
Reply to this message Reply
> the basic case of delegation requires writing, eg.:
> void method1() { pObject_m->method1() }
> -- which is simpler than what you have. so you must be
> aiming at something else.
>
> so i feel there is some larger generalisation/problem not
> explained.

Yes, I skipped the motivation, which was probably a bad idea.

The main motivating problem is "automated" delegation. Given a single large interface (with many function) which is delegated frequently, the technique outlined can be used to make delegation easier. You only have to define one delegation struct.

The other motivator is extension functions. Extension functions only operate on the interface. You can place them in a delegation struct, and get them for free in a delegating struct.

Finally there is the possibility to easily evaluate preconditions / postconditions within the delegation.

To be honest, the technique is hairy, but it is the best I can do in C++ to emulate features in Heron.

Jim Bish

Posts: 1
Nickname: jimbish
Registered: Jan, 2007

Re: C++ Delegation and Dynamic Inheritance Posted: Jan 26, 2007 12:25 PM
Reply to this message Reply
Christopher, I cannot get this stuff to compile. Do you have a copy that will compile with vs2005 and/or gcc 3.4.3 that you could send me?

Thanks
Jim

Flat View: This topic has 5 replies on 1 page
Topic: C++ Delegation and Dynamic Inheritance Previous Topic   Next Topic Topic: Some Objects are More Equal Than Others

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use