有些时候设计类时,其父类的初始化需要使用当前类的数据成员,先看下面的场景:
#include <streambuf> // for std::streambuf
#include <ostream> // for std::ostream
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 )
{}
//...
};
C++的初始化顺序要求先初始化父类,先调用父类的构造函数,而调用父类构造函数时,buf当时并没有构造。因此类的行为就很难确定。
R. Samuel Klatchko提出了一个方式来解决初始化顺序的问题。基类构造函数的调用依赖基类的继承顺序,将成员移动到基类,然后设置好继承顺序。
#include <streambuf> // for std::streambuf
#include <ostream> // for std::ostream
class fdoutbuf
: public std::streambuf
{
public:
explicit fdoutbuf( int fd );
//...
};
struct fdostream_pbase
{
fdoutbuf sbuffer;
explicit fdostream_pbase( int fd )
: sbuffer( fd )
{}
};
class fdostream
: private fdostream_pbase
, public std::ostream
{
typedef fdostream_pbase pbase_type;
typedef std::ostream base_type;
public:
explicit fdostream( int fd )
: pbase_type( fd ), base_type( &sbuffer )
{}
//...
};
上述方式可以使用模板的方式来实现,因此就出现了boost中的base_from_member方式。
参考:
http://www.boost.org/doc/libs/1_46_1/libs/utility/base_from_member.html
分享到:
相关推荐
主要介绍了C++ boost::asio编程-同步TCP详解及实例代码的相关资料,需要的朋友可以参考下
离线安装包,亲测可用
作为C++的准标准库,在C++的开发中的地位已经变得很重要了,自从发现了这本书后,我就开始迫不及待看起来。但是,从网上下载的许多pdf都是没有目录的,要么就是加密了的,所以我下载下来经过几天的编辑,终于做好了...
Boost-Graph:Boost-Graph的发行历史
matlab 2014b 破解缺少dll,boost_date_time-vc100-mt-1_49.dll,以及boost_filesystem-vc100-mt-1_49.dll
boost_date_time-vc100-mt-1_49.dll
FDFS 安装过程
通过boost::asio::serialport类实现串口通信的例子
boost159-locale-1.59.0-2.el7.x86_64.rpm
mysq5.7源码安装的依赖库:boost-1-59-0.tar.gz Boost提供了一组免费的经过同行评审的可移植C ++源库。它包括用于线性代数,伪随机数生成,多线程,图像处理,正则表达式和单元测试的库
matlab 2016a 由于找不到 boost_date_time-vc120-mt-1_56.dll,无法继续执行代码。重新安装程序可能会解决此问题。
boost-thread-1.41.0-11.el6_1.2.i686.rpm是centos工具包。
boost_histogram-1.1.0-cp37-cp37m-win_amd64
Boost C++ Application Development Cookbook - Second Edition
Boost库导引 方便查看,剩下的说明文字,仅仅是为了CSDN上传资源时凑字,谢谢。
Supporting all basic and many advanced features of vi/vim inside Visual Studio, it's a boost for your development productivity. ViEmu for Visual Studio "Classic" is compatible with Visual Studio ...
《xapp1206-boost-sw-performance-zynq7soc-w-neon.pdf》 xilinx开发手册,指导手册.官方文档
该文件为boost库chm版中文帮助文档,版本为boost1.41,其目录结构明确,内容详尽,可谓boost库开发必备电子书籍,希望对使用boost库开发的人员有所帮助。