-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks for all the replies.

They brought me on the right track.

The solution I've got now is also known as the "Barton and Nackman Trick".

It's a bit pervert - but totaly legal C++ code:

template<typename leaftype>
class A
{
  leaftype& asLeaf()
  { return static_cast<leaftype&>(*this); }

public:
  bool foo( leaftype bar )
  { return asLeaf().foo( bar ); }
};

class B : public A<B>
{
public:
  bool foo( B bar )
  { return /*...*/; }
}


The Barton and Nackman trick is important to avoid virtual function
where they are too slow (i.e. when the additional pointer lookup hurts
performance)

CU,
Christian

Paul Kahler schrieb:
> It sounds to me like you should just drop the virtual function
> declaration in the abstract "A" class (or make it non-virtual). It
> serves no purpose other than trying to enforce your design on the
> subclasses. Since you require the foo() function to take a parameter of
> the subclass type, only a B can foo a B, and only a C can foo a C. Let
> us not use complex language constructs (templates for example) just to
> allow the compiler to check that you're following your own convention.
> Put a comment in the base class that defines this requirement on
> subclasses and call it a day. OTOH, if generic code is going to tell an
> X to foo another X (somehow knowing they are the same type) this might
> not work. Hmmmm. I think another option is to declare A:foo(A p1) and
> then have the B class explicitly cast the parameter p1 to a B inside
> B:foo when using the B-specific functionality of the parameter. I don't
> know how to do templates, so this is what I might do until it got too
> ugly.
> 
> Hope that helps,
> Paul
> 
> On Sat, 2005-01-15 at 15:12 +0100, Christian Mayer wrote:
> 
>>-----BEGIN PGP SIGNED MESSAGE-----
>>Hash: SHA1
>>
>>Hi,
>>
>>can someone help me to solve thise problem:
>>
>>Imagine I've got this class hierachy:
>>
>>class A
>>{
>>  virtual bool foo( A bar ) = 0;
>>}
>>
>>class B : A
>>{
>>  bool foo( B bar )
>>  {
>>    ...
>>  }
>>  ....
>>}
>>
>>int main( void )
>>{
>>  B foobar;
>>}
>>
>>this won't compile as my class B is still abstract as I didn't provide a
>>"bool foo( A bar )" member.
>>
>>But I only want class A to be an interface that tells everybody what to
>>expect from it's derivated classes. And one of these things is, that
>>every child must have a member that is called "foo" and has one
>>parameter of the type of the child itself.
>>
>>How do I achieve that?
>>
>>
>>The only workaround I can come up with is, that I have:
>>class B
>>{
>>  friend bool foo( class B, class B );
>>}
>>
>>bool foo( class B bar1, class B bar2 )
>>{
>> ...
>>}
>>
>>I this doesn't guarantee me, that every child of A must have a foo
>>function...
>>
>>Thanks,
>>Christian
> 
> 
> 
> 
> _______________________________________________
> Flightgear-devel mailing list
> Flightgear-devel@flightgear.org
> http://mail.flightgear.org/mailman/listinfo/flightgear-devel
> 2f585eeea02e2c79d7b1d8c4963bae2d
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB7nR5lhWtxOxWNFcRArQdAJ9CFcrVXvlEeUszuzoRmbB/ACJU6wCfdsSA
RYAAKsDENV2fQj0Qn5d2KpE=
=pyGn
-----END PGP SIGNATURE-----

_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@flightgear.org
http://mail.flightgear.org/mailman/listinfo/flightgear-devel
2f585eeea02e2c79d7b1d8c4963bae2d

Reply via email to