473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to define an global object like cout?

Hi,

I want to define an unique file object, like cout, where there is the
only such one in the program.

It seems that singleton pattern can be used for this purpose. But I'm
wondering if cout is an singleton. I would guess not. There were some
threads in the newsgroup discuss about cout and singleton, which is
too long for me to catch the main point. Can somebody give me a brief
introduction on this? And show me how to define an global file object?

Thanks,
Peng

Oct 19 '07 #1
3 2638
Pe*******@gmail.com wrote:
Hi,

I want to define an unique file object, like cout, where there is the
only such one in the program.

It seems that singleton pattern can be used for this purpose. But I'm
wondering if cout is an singleton. I would guess not. There were some
threads in the newsgroup discuss about cout and singleton, which is
too long for me to catch the main point. Can somebody give me a brief
introduction on this? And show me how to define an global file object?
.....
// public header file myiostream.h

#include <iostream>

namespace MYSPACE
{
extern std::ostream & MYOSTREAM;
} // end namespace
.....
.....
// implementation file - myiostream.cpp

#include "myiostream.h"
#include <fstream>

namespace { // anon namespace
struct my_fstream : std::fostream
{
std::fostream m_fostream;
my_fstream()
: m_fostream( "file name" );
{
// do whatever you want to open the file before main()
// is called here
}
};

my_fstream s_mostream;

}// end anon namespace

namespace MYSPACE
{
// create object - mostream
std::ostream & MYOSTREAM = s_mostream;
} // end namespace

.....

///// not tested in a compiler - I probably broke somthing but you
///// should get the drift
Oct 19 '07 #2
Gianni Mariani wrote:
Pe*******@gmail.com wrote:
>Hi,

I want to define an unique file object, like cout, where there is the
only such one in the program.

It seems that singleton pattern can be used for this purpose. But I'm
wondering if cout is an singleton. I would guess not. There were some
threads in the newsgroup discuss about cout and singleton, which is
too long for me to catch the main point. Can somebody give me a brief
introduction on this? And show me how to define an global file object?

....
// public header file myiostream.h

#include <iostream>

namespace MYSPACE
{
extern std::ostream & MYOSTREAM;
} // end namespace
....
....
// implementation file - myiostream.cpp

#include "myiostream.h"
#include <fstream>

namespace { // anon namespace
struct my_fstream : std::fostream
{
std::fostream m_fostream;
my_fstream()
: m_fostream( "file name" );
{
// do whatever you want to open the file before main()
// is called here
}
};

my_fstream s_mostream;

}// end anon namespace

namespace MYSPACE
{
// create object - mostream
std::ostream & MYOSTREAM = s_mostream;
} // end namespace

....

///// not tested in a compiler - I probably broke somthing but you
///// should get the drift
I have this vague recollection that this will not completely cut it as far
as implementing a resource like std::cin is concerned (the problem being
something along the lines that std::cin can be used in several compilation
units and one cannot decide up front which one shall do the
initialization). I think, there is a trick that goes by the name of Schwarz
counter used in solving the problem. Anyway, I don't remember the details.
Best

Kai-Uwe Bux

Oct 19 '07 #3
Kai-Uwe Bux wrote:
....
I have this vague recollection that this will not completely cut it as far
as implementing a resource like std::cin is concerned (the problem being
something along the lines that std::cin can be used in several compilation
units and one cannot decide up front which one shall do the
initialization). I think, there is a trick that goes by the name of Schwarz
counter used in solving the problem. Anyway, I don't remember the details.
modulo the errors in the code (3 at least), the initialization of the
s_mostream object happens somtime before main() is called. If you need
it initialized before main() then you need to make sure it is
initialized using some different mechanism.
Oct 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peng Yu | last post by:
I want to define a macro #define FILEWR(FILENAME) which can be expanded to #ifndef OUTFILE #define OUTFILE ofstream out; #endif
2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
5
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new(...
10
by: athanasios.silis | last post by:
Hello everyone, i am attempting to make a structure #include "globalVars.h" struct myStruct{ int offset; unsigned char uChars; } saveVars, getVars;
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
1
by: Bryan Parkoff | last post by:
An object can be defined using a class. The class contains variables and functions. It has a pointer to bind variables and functions. If I want to create more than one object. The class can...
6
by: Rajesh | last post by:
I read Global Object's constructor will be called before main() function; In which situation it can be really helpful? Is it good practice use Global object and its constructor ? Thanks,...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.