使用“这个”两个构造函数的指针
考虑这个类的实现:
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
。
我该如何解决这个问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DcachePort
依赖于iewStage
,因此如果您要继续使用现有的DcachePort
构造函数实现,则必须传入 < code>iewStage 作为第三个构造函数参数。(或者编辑现有构造函数以传入
fetchStage
作为第三个参数。)或者,重写
LSQ::name()
,以便它使用来自fetchStage
的信息code> 而不是iewStage
。 (如果你不能,那么你仍然需要传入iewStage
作为构造函数参数`)最后一个建议:如果你可以传递 "is-a" 测试,你可以子类
LSQ
,做这样的事情(不确定我是否有模板语法正确):用于“is-a”测试要通过,MyLSQ 的任何实例都必须能够像 LSQ 一样用于 LSQ 的任何方法(包括构造函数),但您可以添加额外的状态/行为,并覆盖 LSQ 的任何虚拟方法。
DcachePort
has a dependency oniewStage
, so if you are going to continue to use the existingDcachePort
constructor implementation, you will have to pass iniewStage
as a third constructor parameter.(Or edit the existing constructor to pass in
fetchStage
as a third parameter.)Alternatively, rewrite
LSQ::name()
so it uses information fromfetchStage
instead ofiewStage
. (and if you can't, then you'll still have to pass iniewStage
as a constructor parameter`)One final suggestion: If you can pass the "is-a" test, you could subclass
LSQ
, doing something like this (not sure if I have the template syntax right):For the "is-a" test to pass, any instance of MyLSQ has to be able to be used like an LSQ for any of LSQ's methods (including the constructor) but you can add on extra state/behavior, and override any of LSQ's virtual methods.