-2

要将用户定义的数据类型传递到 SystemC 通道模板中,需要将这些数据类型定义为一个类,该类还实现了不同类型的运算符<<===

我需要定义一个sc_fifo例如:

sc_fifo<route_t>

为此,必须route_t按照以下示例编写数据类型。

class route_t
{
    public: 
    route_dir_t                     route_dir; 
    unsigned int                    vc_index; 

    // methods allowing the structure to be passed into SystemC channels
    // constructor
    route_t(route_dir_t _route_dir, unsigned int _vc_index) {
        route_dir = _route_dir; 
        vc_index  = _vc_index; 
    }

    inline bool operator == (const route_t& _route) const {
        return (route_dir == _route.route_dir && vc_index == _route.vc_index); 
    }

    inline route_t& operator = (const route_t& _route) {
        route_dir = _route.route_dir; 
        vc_index  = _route.vc_index; 
        return *this; 
    }

}; // end class route_t 
  1. 为什么 SystemC 需要这样的实现?
  2. 为什么operator=需要返回对对象本身的引用?它只是更新内部成员。
  3. 可以将数据类型定义为 astruct而不是使用实现所需运算符的内部方法吗?
  4. 为什么inline在这种情况下使用?
  5. 返回如何*this等同于在方法声明中返回对对象的引用?
4

2 回答 2

0

operator=预计将返回对类本身的引用,因此您可以执行以下任何操作。

a = b = c;
if (a = b) { } // check the resulting value of a
(a = b).foo();

尽管这些可能不是您期望做的事情,但它遵循让用户定义对象的行为方式与内置对象行为相同的一般准则。

至于返回引用,您必须确保不返回对本地对象的引用,但它具有预期的语义。

于 2015-06-14T13:11:42.823 回答
0

请看以下内容:

Myclass a, b, c;

a=b=c=otherMyclass;

为此,每个都operator=必须返回供链中下一个使用的引用。

此外,您通常应该检查分配不是给自己的

    inline route_t& operator = (const route_t& _route) {
   if (&_route != this)
   {
        route_dir = _route.route_dir; 
        vc_index  = _route.vc_index; 
        return *this; 
    }
}

这将处理:

   a=a;
于 2015-06-14T13:15:08.917 回答