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