c++模板子类调用错误的父构造函数

标签 c++ inheritance constructor

我正在使用 Stroustrup 的包装器模板类:

template<class T, class Pref, class Suf>
class Wrap {
protected: 
    T* p;
    int* owned;
    void incr_owned() { if (owned) ++*owned; }
    void decr_owned() { if (owned && --*owned == 0) { delete p; delete owned; } }

    Pref prefix;
    Suf suffix;
public:
    Wrap(T& x, Pref pr, Suf su)
        :p(&x), owned(0), prefix(pr), suffix(su) { } 

    Wrap(T* pp, Pref pr, Suf su)
        :p(pp), owned(new int(1)), prefix(pr), suffix(su) { } 

    Wrap(const Wrap& a)
        :p(a.p), owned(a.owned), prefix(a.prefix), suffix(a.suffix)
        { incr_owned(); }

我将其子类化以创建线程安全对象:

template<class DSP> class DspWrap : public Wrap<DSP, void(*)(), void(*)()> {
protected:
    CriticalSection* criticalSection;

public:
    DspWrap(DSP& x) : Wrap<DSP, void(*)(), void(*)()>(x, &DspWrap::prefix, &DspWrap::suffix) { 
    }

    DspWrap(DSP* pp) : Wrap<DSP, void(*)(), void(*)()>(pp, &DspWrap::prefix, &DspWrap::suffix) { //compiler error here
    }

但是在创建对象的行中 DspWrap<PpmDsp> wrap = DspWrap<PpmDsp>(new PpmDsp());我收到以下错误 error C2664: 'Wrap<T,Pref,Suf>::Wrap(T &,Pref,Suf)' : cannot convert parameter 1 from 'PpmDsp *' to 'PpmDsp &'

但为什么它会调用错误的构造函数呢?实际上有一个 PpmDsp* 的构造函数,那么为什么它会尝试调用 PpmDsp&

提前致谢

最佳答案

我不确定什么你正在尝试将成员初始化为自己,但是你需要为基类和基类成员 *他们自己* 不是这样做的方法。

一旦我为 prefixsuffix 声明了两个 real 函数,其余的就可以正常工作,并且基本构造函数会正确初始化。因为我没有你对 CriticalSectionDSP 的定义,所以我不得不伪造这个示例,但是......

#include <iostream>

typedef int CriticalSection;

template<class T, class Pref, class Suf>
class Wrap {
protected:
    T* p;
    int* owned;
    void incr_owned() { if (owned) ++*owned; }
    void decr_owned() { if (owned && --*owned == 0) { delete p; delete owned; } }

    Pref prefix;
    Suf suffix;
public:
    Wrap(T& x, Pref pr, Suf su)
        :p(&x), owned(0), prefix(pr), suffix(su) { }

    Wrap(T* pp, Pref pr, Suf su)
        :p(pp), owned(new int(1)), prefix(pr), suffix(su) { }
};

template<class DSP> class DspWrap : public Wrap<DSP, void(*)(), void(*)()> {
protected:
    CriticalSection* criticalSection;

    // implemenations of these
    static void prefix_fn() {};
    static void suffix_fn() {};

public:
    DspWrap(DSP& x)
        : Wrap<DSP, void(*)(), void(*)()>(x, &prefix_fn, &suffix_fn)
        , criticalSection(new CriticalSection)
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }

    DspWrap(DSP* pp)
        : Wrap<DSP, void(*)(), void(*)()>(pp, &prefix_fn, &suffix_fn)
        , criticalSection(new CriticalSection)
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
};

struct MyDSP { };

int main()
{
    MyDSP dsp;
    DspWrap<MyDSP> wrap1(dsp);

    MyDSP *dsp2 = new MyDSP;
    DspWrap<MyDSP> wrap2(dsp2);
    return 0;
}

输出

DspWrap<MyDSP>::DspWrap(DSP &) [DSP = MyDSP]
DspWrap<MyDSP>::DspWrap(DSP *) [DSP = MyDSP]

关于c++模板子类调用错误的父构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19008604/

相关文章:

c++ - 为什么 QMediaGaplessPlaybackControl 不存在?

java - 避免在 Java 中使用 instanceof

c++复制构造函数中的用户定义成员

c++ - 我今天应该使用 TCHAR 吗?

c++ - 如何在C++中将字符串转换为int?

c++ - 使用 cmake 传递复合编译器选项

c++ - 为什么通过公开继承另一个类来构建一个类是一种糟糕的做法?

java - 调用抽象类的构造函数

c++ - union 类型数据成员的初始化

C++:结构的构造函数?