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 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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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;
|
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;
?
| |
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*...
|
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...
|
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
|
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:
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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
| |