使用“这个”两个构造函数的指针

发布于 2024-12-23 11:06:56 字数 1932 浏览 4 评论 0 原文

考虑这个类的实现:

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

我该如何解决这个问题?

Consider this class implementation:

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";   
 }

So DcachePort() takes 'this' which is actually

 LSQ(O3CPU *cpu_ptr, IEW *iew_ptr);

Now I added my own constructor:

 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)
  {
  }

Problem is, 'this' in my constructor is

  LSQ(O3CPU *cpu_ptr, Fetch *f_ptr)

and when it enters DcachePort(this) and then name(), it tries to execute

  return iewStage->name() + ".lsq"; 

but in my constructor, iewStage is not initialized. Instead fetchStage is used.

How can I fix that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2024-12-30 11:06:56

DcachePort 依赖于 iewStage,因此如果您要继续使用现有的 DcachePort 构造函数实现,则必须传入 < code>iewStage 作为第三个构造函数参数。

(或者编辑现有构造函数以传入 fetchStage 作为第三个参数。)

或者,重写 LSQ::name(),以便它使用来自 fetchStage 的信息code> 而不是 iewStage。 (如果你不能,那么你仍然需要传入 iewStage 作为构造函数参数`)


最后一个建议:如果你可以传递 "is-a" 测试,你可以子类 LSQ,做这样的事情(不确定我是否有模板语法正确):

template <class Impl>
class MyLSQ : public LSQ<Impl>
{
     Fetch *fetchStage;
     MyLSQ(O3CPU *cpu_ptr, IEW *iew_ptr, Fetch *f_ptr);
}


template <class Impl>
MyLSQ<Impl>::MyLSQ(O3CPU *cpu_ptr, IEW *iew_ptr, Fetch *f_ptr)  
  : LSQ(cpu_ptr, iew_ptr), fetchStage(f_ptr)
{
}

用于“is-a”测试要通过,MyLSQ 的任何实例都必须能够像 LSQ 一样用于 LSQ 的任何方法(包括构造函数),但您可以添加额外的状态/行为,并覆盖 LSQ 的任何虚拟方法。

DcachePort has a dependency on iewStage, so if you are going to continue to use the existing DcachePort constructor implementation, you will have to pass in iewStage 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 from fetchStage instead of iewStage. (and if you can't, then you'll still have to pass in iewStage 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):

template <class Impl>
class MyLSQ : public LSQ<Impl>
{
     Fetch *fetchStage;
     MyLSQ(O3CPU *cpu_ptr, IEW *iew_ptr, Fetch *f_ptr);
}


template <class Impl>
MyLSQ<Impl>::MyLSQ(O3CPU *cpu_ptr, IEW *iew_ptr, Fetch *f_ptr)  
  : LSQ(cpu_ptr, iew_ptr), fetchStage(f_ptr)
{
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文