Can a cast operator be explicit?

Yes and No. It depends on which version of C++, you’re using. C++98 and C++03 do not support explicit type conversion operators But C++11 does. Example, struct A { //implicit conversion to int operator int() { return 100; } //explicit conversion to std::string explicit operator std::string() { return “explicit”; } }; int main() { A … Read more

C++ overload operator [ ][ ]

You cannot overload operator [][], but the common idiom here is using a proxy class, i.e. overload operator [] to return an instance of different class which has operator [] overloaded. For example: class CMatrix { public: class CRow { friend class CMatrix; public: int& operator[](int col) { return parent.arr[row][col]; } private: CRow(CMatrix &parent_, int … Read more