我正在使用 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&
呢?
提前致谢