使用派生类成员初始化父类

时间:2015-05-25 12:30:08

标签: c++ initialization derived-class initializer-list

我想初始化从类B派生的类A,并在B中我首先构造一个用于构造{的缓存{1}},例如,

A

会起作用,因为父对象class B: public A { public: B(): A(cache_), cache_(this->buildCache_()) protected: const SomeDataType cache_; private: SomeDataType buildCache_() const { // expensive cache construction } } 总是先被初始化(填充A之前)。

(为了完整起见:cache_在从cache_派生的类中使用了很多次。)

作为替代方案,我可以做到

B

这样做的缺点是class B: public A { public: B(): A(this->buildCache_()) protected: const SomeDataType cache_; private: SomeDataType buildCache_() { // expensive cache construction // fill this->cache_ explicitly return cache_; } } 不能buildCache_()。此外,海湾合作委员会抱怨

const

对此有更合适的解决方案吗?

2 个答案:

答案 0 :(得分:4)

你正在做什么是未定义的行为,来自[class.base.init] / 14,强调我的:

  

可以为正在构建的对象调用成员函数(包括虚拟成员函数,10.3)。   类似地,正在构造的对象可以是typeid运算符(5.2.8)或dynamic_cast(5.2.7)的操作数。但是,如果这些操作是在 ctor-initializer (或直接调用的函数)中执行的   或者在基类的所有 mem-initializer 完成之前,间接来自 ctor-initializer 结果   操作未定义

您要做的是使用Base-from-Member idiom

class Cache {
protected:
    const SomeDataType cache_;
};

class B : private Cache, public A {
public:
    B() : Cache(), A(cache_)
    { }
};

答案 1 :(得分:1)

从C ++ 11开始,您可以使用正向构造函数:

string encodedString = HttpServerUtility.UrlEncode(yourString);
相关问题