Hello, please help, point out errors in the implementation of tassirovka. I can not understand the principle of operation of #ifdef , #else and #define .

I implement class Trace.h with a template method for writing. In it I implement my macros. Can you please tell me if I'm on the way? The file TRACE.txt is in the same place as the executable file.

Bottom line: does not write anything to the file.

I think that the error in the definition of macros.

Code:

(Trace.h)

 #include <iostream> #include <fstream> class Trace { public: Trace() { std::ofstream TraceFileWrite("TRACE.txt"); } template <class T> void writingData(T object) { TraceFileWrite << T; } ~Trace() { // TraceFileWrite.close(); } }; #ifdef Enabled_Trace #define Enabled_Trace(object) writingData(object) #else #define Enabled_Trace(object) #endif 

(Source.cpp)

 #include "Trace.h" #include <iostream> #include <string> int main() { int a=7; double b=93.09432; std::string str("Hello"); Enabled_Trace(a) Enabled_Trace(b) Enabled_Trace(str) std::cout<<"Hello Trace!"<<std::endl; system("pause"); return 0; } 
  • It looks like you are in the constructor stack creating an instance of TraceFileWrite class std::ofstream , which is destroyed when you exit the constructor. - user239133
  • @AlexanderZonov think so happens, as you said. Then it is better to create an object in private access and work further with it in the constructor. And can you tell me if the macro implementation is correct? I, frankly, are not sure of its correctness? - Nikita Gusev
  • "Total: does not write anything to the file." You are making something up. What you brought here is not compiled in principle. Therefore, the result is only one - the code is not compiled. - AnT

1 answer 1

I think you wanted to do something like this:

 #include <iostream> #include <fstream> #include <string> #define Enable_Trace #if defined(Enable_Trace) class Trace { public: static Trace& tracer() { static Trace t; return t; } template <class T> void writingData(const T& object) { TraceFileWrite << object; } private: Trace() { TraceFileWrite.open("TRACE.txt"); // Проверки, что открыт } ~Trace() { TraceFileWrite.close(); } std::ofstream TraceFileWrite; }; #define doTrace(x) { Trace::tracer().writingData(#x" = "); \ Trace::tracer().writingData((x)); \ Trace::tracer().writingData("\n"); } #else #define doTrace(x) #endif int main() { int a=7; double b=93.09432; std::string str("Hello"); doTrace(a); doTrace(b); doTrace(str); std::cout<<"Hello Trace!"<<std::endl; system("pause"); return 0; } 

I hope you can figure out how and what works here, and you will spread them out by file. In short - if Enable_Trace defined - there is a class Trace and a macro is defined to use it. Class - Singleton (trace file only).

I will add only that I would probably not have overwritten the trace file, but rather made it overwritten.

  • Yes, this is what I wanted. Thank you, you really helped! Everything is clear to me, except for the static static method static Trace& tracer() what does it serve? - Nikita Gusev
  • Could I do without it? How do I know that a static function is needed when this method applies to the class as a whole, and not to someone separate object. - Nikita Gusev
  • Check out what the "Singleton design pattern" is. The point is that you should have one and only one object of the class of trace, otherwise you will start to spoil the trace file yourself. It would be possible to make this function free, not belonging to the class - but in this way we make it clear that it is tightly connected with this class. It seems to me that this is more convenient ... - Harry
  • thanks again, everything is clear, I will analyze the subtleties! - Nikita Gusev