#Programming Micro Note 20161004
##Purpose of void
Source:
Why is (void) 0 a no operation in C and C++?
What is the purpose of the statement “(void)c;”?
Q: Why no operation is defined as ?
#define noop ((void) 0)A: To avoid a psychopath doing something like
int x = noop;compiler complains.
GCC error: void value not ignored as it ought to be
Visucal C++ 'void' illegal with all types
Q: What is the purpose of the last line?
op_queue<operation> completed_ops;
task_cleanup c = { this, &lock, &completed_ops };
(void)c; // EH?A: Maybe to avoid an unused variable warning of c.
Source:
What is the difference between defining an overloaded operator outside the class and defining it inside the class?
c++ two versions of overloading subscript operator
Q: Difference between defining overloaded operators outside and inside the class?
A: Here are the reasons:
- LHS and RHS are symmetric. If we do it inside,
Complex + doublecan compile butdouble + Complexcannot. - Hide class implementation details.
- Generally speaking, prefer non-member functions in C++
Q: Why const version of e.g. subscript operator[], begin(), end() ? Any difference between
const T& operator[](size_t idx) const;and
T& operator[](size_t idx);A: They are for const version of object. For example,
const Vector<int> v = /*Initialization*/;
v[0] = 3; // Wrong!
int n = v[0]; // Even reading is not allowed!if there is no const version of operator[], subscipt operator on const object is not allowed at all (compilation error.) BTW, the two versions are overloaded by constness of function, not by the constness of return type. Funcion cannot be overloaded just by different return types.