From -8094533082709075654
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,28671cb074853115
X-Google-Attributes: gidf78e5,public
From: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Subject: Re: offsetof macro, can it be used in non-trivial classes?
Date: 1997/01/15
Message-ID: <32DCB1F3.7FBF1132@physik.tu-muenchen.de>#1/1
X-Deja-AN: 210199627
references: <01bbead3$6fbf55f0$c07797cd@hacknowledge> <VA.0000000f.000b8656@enterprise> <32c50648.4321059@news.ma.ultranet.com> <199612271843.KAA06170@taumet.eng.sun.com> <u681kw423.fsf@kastle.com> <199612301824.KAA08213@taumet.eng.sun.com> <uiv5fszmt.fsf@kastle.com> <rf5afqn6kbj.fsf@vx.cit.alcatel.fr> <32d518a4.4786494@news.ma.ultranet.com>
x-original-date: Wed, 15 Jan 1997 11:31:15 +0100
organization: [posted via] Leibniz-Rechenzentrum, Muenchen (Germany)
x-auth: PGPMoose V1.1 PGP comp.std.c++
newsgroups: comp.std.c++
originator: austern@isolde.mti.sgi.com
x-mailer: Mozilla 3.0Gold (X11; I; Linux 2.0.26 i586)


Pablo Halpern wrote:
> 
> James Kanze <james-albert.kanze@vx.cit.alcatel.fr> wrote:
> 
> [ as part of solution to avoid using offsetof ]
> >
> >    class thing
> >       : private specialized_callback_1
> >       , private specialized_callback_2
> >
> 
> This works fine if the two classes are different, but I have a situation
> in which both classes are the same. Since you can't inherit immediately
> from the same class twice, I was forced to use one of the following
> solutions:
> 
> class thing;
> class specialized_callback : public callback
> {
>   public: specialized_callback(thing* p) : parent(p) { ... }
>   private: thing* parent;
>   ...
> };
> 
> class thing
> {
>   specialized_callback c1;
>   specialized_callback c2;
> public:
>   thing() : c1(this), c2(this) { ... }
> };
> 
> OR
> 
> class specialized_callback1 : public specialized_callback { ... };
> class specialized_callback2 : public specialized_callback { ... };
> 
> class thing : private specialized_callback_1,
>               private specialized_callback_2
> {
> ...
> };
> 
> For my application, I chose the first option, partially because it
> avoided the forwarding constructors needed for the second option and
> seemed at the time easier to read and comment. The second solution has
> the advantage of not carrying around the extra pointer to the parent
> thing object.
> 

what about the following solution:

class thing;

template<int which> class specialized_callback: public callback
{
// ... (code doesn't depend on which)
};

class thing: private specialiced_callback<1>,
             private specialized_callback<2>
{
// ...
};

The trick is, that the compiler expands the template into two
different, but identic classes.
You can even give some information as template parameter,
which you would have given as constructor parameter normally,
f.ex.:

template<int event> class specialized_callback: public callback
{
  specialized_callback() { register(*this,event); }
// ...
};

class thing: private specialized_callback<event1>,
             private specialized_callback<event2>
{
// ...
};

[...]
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



