Johan Nilsson
12/9/2004 1:13:00 PM
"Robert Klemme" <bob.news@gmx.net> wrote in message
news:31qukrF3d6rb9U1@individual.net...
>
> "Johan Nilsson" <johan.nilsson@---.esrange.ssc.se> schrieb im Newsbeitrag
> news:1102594375.344eddcc1c357ac539046c764cd8c6b8@teranews...
> > Hi,
> >
> > I've got a question regarding inheritance, mixins, order of
> initialization
> > and calling module/super class "initializers". I'd like to do something
> > along the lines of (pseudo-ruby code):
> >
[snip]
>
> I don't know which real problem you are trying to solve. I can think of
> several other approaches. You might want to store your mixin created
> instances directly in instance variables and record only the names of
> these.
Thanks for your reply(s), I'll check them out.
I'm actually mapping a legacy C++ construct to Ruby (no, this is not the
actual code and probably contain some typos):
---
struct FooBarBase {};
struct Foo : FooBarBase {};
struct Bar : FooBarBase {};
class Base
{
protected:
Base(int stuffs)
: m_stuff(stuffs, boost::shared_ptr<FooBarBase>())
{}
void add_stuff_at(int index, boost::shared_ptr<FooBarBase> pfb)
{ m_stuff.at(index) = pfb; }
public:
... functions operating on all "stuff" as a homogenous collection ...
private:
std::vector<boost::shared_ptr<FooBarBase> > m_stuff;
};
template<typename Mixee, int which>
struct FooMixin
{
FooMixin()
: m_pFoo(boost::shared_ptr<Foo>(new Foo))
{ return static_cast<Mixee*>(this)->add_stuff_at(which, m_pFoo); }
const Foo& foo() const
{ return *m_pFoo; }
private:
boost::shared_ptr<Foo> m_pFoo;
};
template<typename Mixee, int which>
struct BarMixin
{ ... };
struct Derived
: Base
, FooMixin<Derived, 0>
, BarMixin<Derived, 1>
{
Derived()
: Base(2)
{}
};
int main(int, char*[])
{
Derived d;
const Foo& foo = d.foo();
d.bar();
return 0;
}
---
To further complicate matters I was trying to use SWIG to inherit from the
C++ class "Base" using the cross-language polymorhism feature. Now I believe
I'll rewrite this part in Ruby instead (but the FooBaseBase stuff etc will
still be wrapped from C++).
Thanks again
// Johan