1
考虑这个类实现:使用this指针为两个构造
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()
需要 '这个' 这其实是
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)
{
}
问题是,“这”在我的构造函数是
LSQ(O3CPU *cpu_ptr, Fetch *f_ptr)
,当它进入DcachePort(this)
然后name()
,它试图执行
return iewStage->name() + ".lsq";
但在我的构造函数,iewStage
未初始化。而是使用fetchStage
。
我该如何解决这个问题?
你能粘贴一个片段吗?这两个构造函数调用'DcachePort'用'this',但端口()不能区分 – mahmood 2011-12-28 18:29:26
你怎么可能有两个构造函数,其中一个初始化iewStage但不fetchStage,而另一个初始化fetchStage但不iewStage?这听起来像你需要一个构造函数来初始化两者。 – 2011-12-28 18:31:50
好吧,让我试试“作为第三个参数传递” – mahmood 2011-12-28 18:34:45