Advertisement

My first class - conditional compilation question

Started by November 23, 2000 10:27 AM
1 comment, last by Moot 24 years, 1 month ago
I wrote my first class! And it works!! I'm going to put the source at the end of the post, so if anyone fancies looking it over and pointing out anything I'm doing wrong then please do so. Be kind though - I'm new to this! The class is a very simple log file writer to aid debugging of my DirectX code. The constructor creates/opens the log file. The print method writes a line of text to the file. The destructor closes the file. Since writing it I have seen the equivalent class in CDX, and I've decided to improve my version by allowing variable arguments to be passed to the print method and using these in the sprintf statement, which I guess will mean I will not need to overload the print method for each variable type. Something else I noticed is that the CDX log class uses conditional compilation to only compile the logging stuff if a 'debug flag' has been defined. I'm finding this a little confusing. Adapting the CDX code to my class I'd end up with something like: #ifdef DEBUG_MODE #define PRINT_DEBUG_LINE myDebugObject.print #else #define PRINT_DEBUG_LINE ((void)0) #endif OR.... #ifdef DEBUG_MODE #define PRINT_DEBUG_LINE myDebugObject.print #else #define PRINT_DEBUG_LINE #endif How does this work? I understand the principal that, if debugging, PRINT_DEBUG_LINE will be replaced by 'myDegugObject.print', whereas if not debugging either nothing or '((void)0)' will be used instead. Doesn't this mean that if I hadn't defined DEBUG_MODE then a line such as PRINT_DEBUG_LINE("blah blah blah"); would end up as ("blah blah blah"); or ((void)0)("blah blah blah"); How does the compiler deal with this? Is the line skipped completely? Which version should I use, '((void)0)' or just leaving it blank? I'm using MSCV 6.0. (EDIT NOTE: What's up with multiple source tags? This looked a mess first time round so I've changed it) (2nd EDIT NOTE: AAARGHHHH! How do you turn off the auto-smiley thing?! I've had to put a space between the :: and the 'p' of print for now) Cheers, Moot (Here's the source for my class...)
    
// Header file:


#ifndef DEBUG_FILE_PRINTER
#define DEBUG_FILE_PRINTER

#include <windows.h>
#include <stdio.h>
#include <string>

using namespace std;

class cDebugFilePrinter
{
public:
	cDebugFilePrinter(const char *);	// Constructor

	~cDebugFilePrinter();		// Destructor


	void Print(const char *);	// Print string to file

	void Print(int);	// Print int to file

	void Print(string); // Print string to file

	void Print(double); // Print double to file


private:
	FILE*  DebugFile;	// Filename for the log file

};

#endif

// CPP file:


#include "Error.h"
#include <stdio.h>
#include <string>

using namespace std;

cDebugFilePrinter::cDebugFilePrinter(const char *FileName) 
{
	DebugFile = fopen(FileName,"wt");
//	fprintf(DebugFile, "Debug constructor\n");

}

cDebugFilePrinter::~cDebugFilePrinter()
{
//	fprintf(DebugFile, "Debug destructor\n");

	fclose(DebugFile);
}

void cDebugFilePrinter:: print(const char *str)
{
	if (DebugFile != NULL) 
	{
		fprintf(DebugFile, "%s\n", str);
	}
}

void cDebugFilePrinter:: print(int i)
{
	if (DebugFile != NULL) 
	{
		fprintf(DebugFile, "%d\n", i);
	}
}

void cDebugFilePrinter:: print(string s)
{
	if (DebugFile != NULL) 
	{

		fprintf(DebugFile, "%s\n", s.c_str());
	}
}

void cDebugFilePrinter:: print(double d)
{
	if (DebugFile != NULL) 
	{

		fprintf(DebugFile, "%g\n", d);
	}
}
    
Edited by - Moot on 11/23/00 10:31:45 AM Edited by - Moot on 11/23/00 10:33:57 AM Edited by - Moot on 11/23/00 10:35:41 AM Edited by - Moot on 11/23/00 11:03:36 AM
#ifdef DEBUG_MODE
#define PRINT_DEBUG_LINE(p) myDebugObject.print(p)
#else
#define PRINT_DEBUG_LINE()
#endif

Advertisement
Oh yeah, thanks Ford, that''s a much more obvious way to do it!

This topic is closed to new replies.

Advertisement