P_ifs->read((char *)&obj.cM圜har, sizeof(obj.cM圜har)) īut, of course, that will cost you the price of a dynamic-cast, which may not be much. P_ifs->read((char *)&obj.dMyDouble, sizeof(obj.dMyDouble)) P_ifs->read((char *)&obj.fMyFloat, sizeof(obj.fMyFloat)) P_ifs->read((char *)&obj.iMyInt, sizeof(obj.iMyInt)) Std::ifstream* p_ifs = dynamic_cast(&is) P_ofs->write((const char *)&obj.cM圜har, sizeof(obj.cM圜har)) įriend std::istream& operator>(std::istream& is, CBase& obj) P_ofs->write((const char *)&obj.dMyDouble, sizeof(obj.dMyDouble)) P_ofs->write((const char *)&obj.fMyFloat, sizeof(obj.fMyFloat)) Os write((const char *)&obj.iMyInt, sizeof(obj.iMyInt)) To solve that problem, you could use a dynamic_cast within the operators: friend std::ostream& operator(&os) This could be problematic if you compose more complicated use-scenarios. The problem with this scheme, however, is that it won't work if, at some point, the file-stream objects get casted to non-file-stream class references. Is.read((char *)&obj.cM圜har, sizeof(obj.cM圜har)) Is.read((char *)&obj.dMyDouble, sizeof(obj.dMyDouble)) Is.read((char *)&obj.fMyFloat, sizeof(obj.fMyFloat)) Is.read((char *)&obj.iMyInt, sizeof(obj.iMyInt)) So, you could just do this: friend std::ostream& operator>(std::istream& is, CBase& obj)įriend std::ifstream& operator>(std::ifstream& is, CBase& obj) Whenever the stream object is of a class derived from the file-stream classes, then it should pick that overload instead of the non-file-stream overloads, just because the file-stream classes are more derived (specialized) than the non-file-stream classes. Technically, you could just overload the operators for the classes std::ofstream and std::ifstream. is.read((char *)&obj.cM圜har, sizeof(obj.cM圜har)) is.read((char *)&obj.dMyDouble, sizeof(obj.dMyDouble)) is.read((char *)&obj.fMyFloat, sizeof(obj.fMyFloat)) is.read((char *)&obj.iMyInt, sizeof(obj.iMyInt))
#Overload api vba how to#
Please can you show me how to define different operators for each.įriend std::ostream& operator>(std::istream& is, CBase& obj) But the ways in which each need to be implemented is different. I am trying to overload the fstream operators (ofstream/ifstream), so that I can save a class to a 'Binary' file and also display it with cout.