Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ Questions (Partly oo design)

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
0
0
GB
Lo all. I decided that this is the best forum to ask these questions, as they are based on C++. I am trying to make this as portable as possible, but for now am working in MS C++ (.net 2003)

I am working on a new logging system, as I never seem to catch all the errors and conditions I need when I am debugging.

My initial thought was to have a CLog class (Singleton design).
This class will be passed a log file and path in the constructor, and will then look after itsself (using stream io)

In order to use it in my application I can either build up a large string in my application, and just call CLog::Output(String), or I can overide the operator<<, so in my ap I can just type in CLog << SomeData << somemoredata;

I would prefer to use the second method, as I feel it is more oo than writing loads of code to convert data to Strings.

1) So far I have created 2 operator<< functions, one taking a std::string and one taking a DWORD variable.
I cannot create another one that will take a wchar_t variable. The system implies that it is looking for a class in the parameter list, but a DWORD isn't a class and that one works. Can anyone explain why ?(Headers below)
CLogger& operator<<(const std::string T);
CLogger& operator<<(const DWORD T);
CLogger& operator<<(const wchar_t* T);

2) In order to try and streamline my code, and to prevent me having to create loads of operator<<'s to pass in to my object, is there any way I can create a template function like you can do with classes ? (apologies if this is a stupid question, but I have no books on my desk at present)

3) In order to make the class self contained, I envisaged the following happening.
a) CLog Instantiated -> Creates file, opens file Outputs header and closes file.
b) << to file -> Open File if not already open, write data
*do not close the file here, as if there are multiple <<'s incoming, thats a lot of unnecessary open() and close() statements* repeat b until an explicit closefile() command is sent from the main applicaiton.

In order to do this, I need to make the filestream persist between function calls, so I make it a static member.

In order to set the exceptions flags for the stream when I open it for output, I also need a pointer to the stream. But if I add the following code to my function, it barfs up errors at me.

Anyone know why this isn't allowed, and if I can get round the problem ? ie in myclass.h
private:
std::eek:fstream* ptrFS;
std::eek:fstream FS;

and in my CLog::CLog()
{
ptrFS = &FS;
}

b)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top