More C++ Idioms/Base-from-Member
Base-from-Member
[edit | edit source]Intent
[edit | edit source]To initialize a base class from a data-member of the derived class.
Also Known As
[edit | edit source]Motivation
[edit | edit source]In C++, base classes are initialized before any member of the derived class. The reason for this is that members of a derived class may use the base part of the object. Therefore, all the base parts (i.e., all base classes) must be initialized before members of the derived class. Sometimes, however, it becomes necessary to initialize a base class from a data member that is available only in the derived class. It sounds contradictory to the rules of C++ language because the parameter (a member of derived class) that is passed to the base class constructor must be fully initialized. This creates a circular initialization problem (an infinite regress).
The following code, obtained from the Boost libraries,[1] shows the problem.
#include <streambuf>
#include <ostream>
namespace std {
class streambuf;
class ostream {
explicit ostream(std::streambuf * buf);
//...
};
}
// A customization of streambuf.
class fdoutbuf : public std::streambuf
{
public:
explicit fdoutbuf( int fd );
//...
};
class fdostream : public std::ostream
{
protected:
fdoutbuf buf;
public:
explicit fdostream( int fd )
: buf( fd ), std::ostream( &buf )
// This is not allowed: buf can't be initialized before std::ostream.
// std::ostream needs a std::streambuf object defined inside fdoutbuf.
{}
};
The above code snippet shows a case where the programmer is interested in customizing the std::streambuf
class. They do so in fdoutbuf
by inheriting from std::streambuf
. The fdoutbuf
class is used as a member in fdostream
class, which is a kind of std::ostream
. The std::ostream
class, however, needs a pointer to a std::streambuf
class, or its derived class. The type of pointer to buf
is suitable but passing it makes sense only if buf
is initialized. However, it won’t be initialized unless all base classes are initialized. Hence the infinite regress. The base-from-member idiom addresses this problem.
Solution and Sample Code
[edit | edit source]This idiom makes use of the fact that base classes are initialized in the order they are declared. The derived class controls the order of its base classes, and in turn, controls the order in which they are initialized. In this idiom, a new class is added just to initialize the member in the derived class that is causing the problem. This new class is introduced in the base-class-list before all other base classes. Because the new class comes before the base class that needs the fully constructed parameter, it is initialized first and then the reference can be passed as usual. Here is the solution using base-from-member idiom.
#include <streambuf>
#include <ostream>
class fdoutbuf : public std::streambuf
{
public:
explicit fdoutbuf(int fd);
//...
};
struct fdostream_pbase // A newly introduced class.
{
fdoutbuf sbuffer; // The member moved 'up' the hierarchy.
explicit fdostream_pbase(int fd)
: sbuffer(fd)
{}
};
class fdostream
: protected fdostream_pbase // This class will be initialized before the next one.
, public std::ostream
{
public:
explicit fdostream(int fd)
: fdostream_pbase(fd), // Initialize the newly added base before std::ostream.
std::ostream(&sbuffer) // Now safe to pass the pointer.
{}
//...
};
int main()
{
fdostream standard_out(1);
standard_out << "Hello, World\n";
return 0;
}
The fdostream_pbase
class is the newly introduced class that now has the sbuffer
member. The fdostream
class inherits from this new class and adds it before std::ostream
in its base class list. This ensures that sbuffer
is initialized before std::ostream
and the pointer can be safely passed to it's constructor.