Class method returning reference C++/SystemC -
this question has answer here:
to pass user-defined datatypes systemc channels templates requires these datatypes defined class implements different kind of operators '<<', '=', '=='. need define sc_fifo such as:
sc_fifo<route_t>
for correct, route_t datatype has written in below example.
class route_t { public: route_dir_t route_dir; unsigned int vc_index; // methods allowing structure passed 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
- why systemc require such implementation?
- why operator '=' need return reference object itself? updates internal members..
- can datatype defined struct instead internal methods implementing required operators?
- why 'inline' used in context?
- how can returning *this equivalent returning reference object expected in declaration of method?
operator=
expected return reference class can of following.
a = b = c; if (a = b) { } // check resulting value of (a = b).foo();
although these may not things expect follows general guideline of having user defined objects behave in same way built in objects behave.
as returning reference, have make sure don't return reference local object has expected semantics.
Comments
Post a Comment