I have a class and I want that when I give it as a parameter for printf/sprintf that it is being used as a specific string (depending on whats going on in the class). Is something like that possible ?
printf("blah blah %s ", customclass);
What do I have to do that this happens without any kind of typecasting and without any kind of necessary function call like customclass.xyz() ?
printfis a variadic function, which cannot be used in any sensible manner with user-defined types.If you’re in C++, you should prefer streams (e.g.
std::cout) toprintffor exactly this sort of reason. With streams, you can overload the<<operator: