473,804 Members | 2,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2653
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
3173
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
1559
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 (objects and scalar-types). In the following example Global is able to create objects of any kind with and index value attached to. So a Global<0, string> is a unique string instance object allocated into the system that can be accessed from any...
5
2403
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( size_t size ); void *operator new( size_t size, AllocationType allocType ); void *operator new( size_t size, MemoryHandler* memHandler ); void *operator new( size_t size, MemoryHandler* memHandler, AllocationType
10
2607
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
2805
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 object of that class. And is that also the reason why if you use class member functions for operators << and >you have to write: someclass << cout; someclass >cin; ?
5
2577
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: const char *s = string_mapper<thetype>(); However I do not know the string to be associated with the type at compile time, and will need a way to set up the mapping, to be created at run time, possibly like so: void foo(char*...
1
2685
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 have two objects with a separate pointer. However, functions are always shared with one or more objects, but each object has its own separate variables. It allows to reduce unreadable messy source code and bugs. However, I do not want to use a...
6
1547
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, Rajesh.S
5
7243
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
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9572
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.