2015-06-14 71 views
-2

要将用户定义的数据类型传递到SystemC通道模板,需要将这些数据类型定义为实现不同类型的运算符'','=','=='的类。 我需要定义sc_fifo如:返回引用的类方法C++/SystemC

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. 为什么运算符'='需要返回对象本身的引用?它只是更新内部成员..
  3. 可以将数据类型定义为结构而不是内部方法实现所需的运算符?
  4. 为什么在此上下文中使用“内联”?
  5. 如何返回*这相当于在方法声明中返回对象的引用?
+0

为什么不问赋值运算符的语义,而不是发布主要是无关紧要的东西吗?这会使问题更清楚。 – juanchopanza

+0

不是每个人都是C++的专家,并感谢您的好评。与你无关,并不会让每个人都无关紧要。我将删除帖子,并参考发布前我试图找到的答案。 – Theo

+0

您的大部分帖子都与您所提问题无关,这会让帖子感到困惑。 – juanchopanza

回答

0

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

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

虽然这些可能不是你所期望做它遵循其用户定义的对象以相同的方式表现为内置对象的行为的一般准则的事情。

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

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; 
+0

谢谢格伦 – Theo