KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am working with an open-source UNIX tool that is implemented in C++, and I need to change some code to get it to do what I want. I would like to make the smallest possible change in hopes of getting my patch accepted upstream. Solutions that are implementable in standard C++ and do not create more external dependencies are preferred. Here is my problem. I have a C++ class -- let's call it "A" -- that currently uses fprintf() to print its heavily formatted data structures to a file pointer. In its print function, it also recursively calls the identically defined print functions of several member classes ("B" is an example). There is another class C that has a member std::string "foo" that needs to be set to the print() results of an instance of A. Think of it as a to_str() member function for A. In pseudocode: class A { public: ... void print(FILE* f); B b; ... }; ... void A::print(FILE *f) { std::string s = "stuff"; fprintf(f, "some %s", s); b.print(f); } class C { ... std::string foo; bool set_foo(std::str); ... } ... A a = new A(); C c = new C(); ... // wish i knew how to write A's to_str() c.set_foo(a.to_str()); I should mention that C is fairly stable, but A and B (and the rest of A's dependents) are in a state of flux, so the less code changes necessary the better. The current print(FILE* F) interface also needs to be preserved. I have considered several approaches to implementing A::to_str(), each with advantages and disadvantages: Change the calls to fprintf() to sprintf() I wouldn't have to rewrite any format strings print() could be reimplemented as: fprint(f, this.to_str()); But I would need to manually allocate char[]s, merge a lot of c strings , and finally convert the character array to a std::string Try to catch the results of a.print() in a string stream I would have to convert al
Tags (comma-separated)
Save Edits
Cancel