Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
problem deriving class from ostringstream (operator <<)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Bob Hairgrove, post: 1523003"] Here's how this is usually done. For simplification, I will assume for now that the data you are outputting is passed as a std::string ... we can handle the other types later. 1. Define an abstract base class (called, for example, "Logger") with a pure virtual function, for example, writeLog(std::string const &). 2. Derive various concrete classes which implement the writeLog() method. You can have a FileLogger, a DbLogger, and a ConsoleLogger, for example. FileLogger writes to the file system, DbLogger does a database update, and ConsoleLogger writes to stdout. You don't necessarily need std::ostream for implementing the writeLog() function -- it makes sense for FileLogger and ConsoleLogger, but probably not for DbLogger. Therefore, I wouldn't inherit from ostream at all. 3. Define a class or subsystem in your application which manages the output. This class, or subsystem, would keep a vector of pointers to base class Logger. When it comes time to log your data, just iterate through the vector and call the writeLog() function on your polymorphic pointer. It is a very bad habit to use macros for this sort of thing. What you REALLY want is an Abstract Factory (google for "design patterns"). Do not use leading underscores in identifiers ... these are "reserved for the implementation" (i.e. the compiler and all the standard libraries). If you never use argc and argv, why include them at all? Not necessarily, you could define a template class and log any type you wish. BTW it's overload, not override: overloading is when different functions with the same name, often in the same class, take different argument types and/or number of arguments, or have different const-ness. Overriding is writing two functions with the same name AND the same arguments and number of arguments, one in a derived class which inherits from a base class containing the other function which must be declared as a virtual function in the base class. You shouldn't use these old headers (ending in *.h) which are there only for backwards compatibility with code which uses them. It is dangerous to mix the newer headers with the older ones exactly because of problems such as the ones you describe below. sstream is newer than the old headers which end with .h, so there isn't one. Yes: [URL]http://www.parashift.com/c++-faq-lite/[/URL] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
problem deriving class from ostringstream (operator <<)
Top