【发布时间】:2011-12-28 18:16:34
【问题描述】:
考虑这个类的实现:
template <class Impl>
class LSQ {
public:
LSQ(O3CPU *cpu_ptr, IEW *iew_ptr);
IEW *iewStage;
class DcachePort : public Port
{
protected:
/** Pointer to LSQ. */
LSQ *lsq;
public:
DcachePort(LSQ *_lsq)
: Port(_lsq->name() + "-dport", _lsq->cpu), lsq(_lsq)
{ }
};
...
};
// default code
template <class Impl>
LSQ<Impl>::LSQ(O3CPU *cpu_ptr, IEW *iew_ptr)
: cpu(cpu_ptr), iewStage(iew_ptr), dcachePort(this),
{
...
}
// default code
template<class Impl>
std::string
LSQ<Impl>::name() const
{
return iewStage->name() + ".lsq";
}
所以DcachePort() 采用“this”,实际上是
LSQ(O3CPU *cpu_ptr, IEW *iew_ptr);
现在我添加了自己的构造函数:
template <class Impl>
class LSQ {
public:
LSQ(O3CPU *cpu_ptr, IEW *iew_ptr); // default code
LSQ(O3CPU *cpu_ptr, Fetch *f_ptr); // added by me
IEW *iewStage;
Fetch *fetchStage;
class DcachePort : public Port
{
protected:
/** Pointer to LSQ. */
LSQ *lsq;
public:
DcachePort(LSQ *_lsq) // default code
: Port(_lsq->name() + "-dport", _lsq->cpu), lsq(_lsq)
{ }
};
...
};
// added by me
template <class Impl>
LSQ<Impl>::LSQ(O3CPU *cpu_ptr, Fetch *f_ptr) // added by me
: cpu(cpu_ptr), fetchStage(f_ptr), dcachePort(this)
{
}
问题是,我的构造函数中的'this'是
LSQ(O3CPU *cpu_ptr, Fetch *f_ptr)
当它进入DcachePort(this)然后进入name()时,它会尝试执行
return iewStage->name() + ".lsq";
但在我的构造函数中,iewStage 未初始化。而是使用fetchStage。
我该如何解决这个问题?
【问题讨论】:
标签: c++ constructor this