Jay Wolfe wrote:
Hello,
I'm guessing I can't do this, but can I do work in a subclass before its
parent class initialization?
I've got 2 classes, class A & subclass B:
class A
{
public:
A(): x(0), y(0) {}
...
}
class B: public A
{
//bunch of definitions
}
So, normally for subclass B I'd initialize as
B::B() : A()
{
do_some_work_here();
}
Is it possible to execute 'do_some_work_here()' BEFORE the parent class
initialization?
Thank you,
Jay
This is a bit hackish, but you could make a separate class that did the
work in its constructor, and derive from that as well. You would be
exploiting the fact that base classes are initialized in order of
declaration. For example:
class do_work
{
public:
do_work() { do_some_work_here() ; }
} ;
class B : public do_work, public A
{
....
} ;
The do_work class will be initialized before A. If you want more
information, see the boost utility template here:
http://boost.org/libs/utility/base_from_member.html
-Alan