I am writing some testing code for an existing C++ application by writing a file with a main() function and some helper classes, and linking it to the object files of the application's other classes. I do not have access to the source files, only to the headers and objects/libraries, which I cannot modify.
In my test code I want to output a subset of fields of a huge structure. Unfortunately, there is already a global ostream& operator<<(ostream&, HugeStruct const&) function in the existing code, which dumps everything. The function is declared in the HugeStruct's header file and implemented in the corresponding source.
Is there a way to override that function only in the testing source without having the compiler and/or linker throw a fit? It is compiled with GCC 4.8.5.
EDIT:
Yes, there are workarounds that I can use.
I could use a print function (as suggested in the comments), but I like the convenience of chaining the << operators.
I can write a std::string out(HugeStruct const&) function that will output the fields to a string and use os << out(hs). This is what I currently do, but it feels like a roundabout way.
Still, I find the question interesting in the general case.
If you can modify the header, to truly override the implementation, you could:
ostream& operator<<(ostream&, HugeStruct const&)as inlineostream& operator<<(ostream&, HugeStruct const&)in a cppHow this works:
inlinetells the linker to not complain if it finds multiple definitionsI do not recommend this :) the
outfunction is safer.Another option I see is to use overload resolution: You have to make sure your local
operator<<is a better match than the library provided one.You can do that by for example deriving from the
ostreamyou use in your test file. Here I will assume you usestd::cout.