Let's say you want to write a simple header file, and don't want to be
burdened with having to provide a source file as well with it.
There's a problem when the need arises for a global object. You could simply
make the object static... but then you'll have a separate object for each
source file that includes the header.
Do many people use a inline function to get around this?
inline
int &GetGlobal()
{
static int i;
return i;
}
--
Frederick Gotham 8 1782
Frederick Gotham wrote:
Let's say you want to write a simple header file, and don't want to be
burdened with having to provide a source file as well with it.
There's a problem when the need arises for a global object. You could simply
make the object static... but then you'll have a separate object for each
source file that includes the header.
Do many people use a inline function to get around this?
inline
int &GetGlobal()
{
static int i;
return i;
}
I don't hope so ;-)
/Peter
peter koch wrote:
>Do many people use a inline function to get around this?
inline int &GetGlobal() { static int i; return i; }
I don't hope so ;-)
That's actually a common technique to make i a Singleton:
inline
SingleClass & getSingleton()
{
static SingleClass sc;
return sc;
}
The benefit is sc is well-defined to construct just before that function
enters. If sc were in the global data space, it would define at a random
time before main(), so any code that uses sc before main() would have
undefined behavior.
Now read "Singletoni tis" in news:comp.objec t to avoid singleton abuse! And
don't put this crap in a header - write an implementation file for it. Some
small C++ applications can get by with header-only implementations , but
large scale C++ applications are bound by their compile times, so
implementation files are crucial.
--
Phlip http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
peter koch wrote:
Frederick Gotham wrote:
>Let's say you want to write a simple header file, and don't want to be burdened with having to provide a source file as well with it.
There's a problem when the need arises for a global object. You could simply make the object static... but then you'll have a separate object for each source file that includes the header.
Do many people use a inline function to get around this?
inline int &GetGlobal() { static int i; return i; }
I don't hope so ;-)
Why not? This is one of singleton implementations . Compare:
class MySingleton {
MySingleton(); // private c-tor, nobody else should construct
public:
static MySingleton& instance() {
static MySingleton s;
return s;
}
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor Bazarov wrote:
peter koch wrote:
Frederick Gotham wrote:
Let's say you want to write a simple header file, and don't want to
be burdened with having to provide a source file as well with it.
There's a problem when the need arises for a global object. You
could simply make the object static... but then you'll have a
separate object for each source file that includes the header.
Do many people use a inline function to get around this?
inline
int &GetGlobal()
{
static int i;
return i;
}
I don't hope so ;-)
Why not? This is one of singleton implementations . Compare:
class MySingleton {
MySingleton(); // private c-tor, nobody else should construct
public:
static MySingleton& instance() {
static MySingleton s;
return s;
}
};
Surely - I know that idiom. But Fredericks question was not about
singletons, but rather one of laziness. And when a singleton is
appropriate, you better write something more elaborate than what
Frederick wrote or you'll get in trouble. If for nothing else, theres
the problem about using the function in multithreaded code. This
suddenly is unsafe even if youre only reading.
So put shortly: don't do what Frederick suggests unless you have given
it good thought.
/Peter
Phlip wrote:
peter koch wrote:
Do many people use a inline function to get around this?
inline
int &GetGlobal()
{
static int i;
return i;
}
I don't hope so ;-)
That's actually a common technique to make i a Singleton:
inline
SingleClass & getSingleton()
{
static SingleClass sc;
return sc;
}
Correct.
>
The benefit is sc is well-defined to construct just before that function
enters. If sc were in the global data space, it would define at a random
time before main(), so any code that uses sc before main() would have
undefined behavior.
Now read "Singletoni tis" in news:comp.objec t to avoid singleton abuse! And
don't put this crap in a header - write an implementation file for it. Some
small C++ applications can get by with header-only implementations , but
large scale C++ applications are bound by their compile times, so
implementation files are crucial.
Right. And it is not just compilation time that is an issue.
/Peter
peter koch wrote:
>
Victor Bazarov wrote:
>peter koch wrote:
Frederick Gotham wrote: Let's say you want to write a simple header file, and don't want to be burdened with having to provide a source file as well with it.
There's a problem when the need arises for a global object. You could simply make the object static... but then you'll have a separate object for each source file that includes the header.
Do many people use a inline function to get around this?
inline int &GetGlobal() { static int i; return i; }
I don't hope so ;-)
Why not? This is one of singleton implementations . Compare:
class MySingleton { MySingleton(); // private c-tor, nobody else should construct public: static MySingleton& instance() { static MySingleton s; return s; } };
Surely - I know that idiom. But Fredericks question was not about
singletons, but rather one of laziness.
That maybe the way he put up the problem. However if your interface is
templated, your maybe forced to put your implementation into the header
(e.g., when your compiler does not support "export").
And when a singleton is
appropriate, you better write something more elaborate than what
Frederick wrote or you'll get in trouble. If for nothing else, theres
the problem about using the function in multithreaded code. This
suddenly is unsafe even if youre only reading.
I can see the problems with multithreading for the first call that needs to
create the variable. However, my eyes fail me for the subsequent calls.
Could you elaborate on this one -- it sounds interesting.
So put shortly: don't do what Frederick suggests unless you have given
it good thought.
Well, name some idiom in C++ for which that does not hold :-)
Best
Kai-Uwe Bux
peter koch posted:
Surely - I know that idiom. But Fredericks question was not about
singletons, but rather one of laziness.
Using a header just involves a simple #include.
Using a source file involves adding it to your "compile files" and so on...
--
Frederick Gotham
Kai-Uwe Bux skrev:
peter koch wrote:
[snip]
And when a singleton is
appropriate, you better write something more elaborate than what
Frederick wrote or you'll get in trouble. If for nothing else, theres
the problem about using the function in multithreaded code. This
suddenly is unsafe even if youre only reading.
I can see the problems with multithreading for the first call that needs to
create the variable. However, my eyes fail me for the subsequent calls.
Could you elaborate on this one -- it sounds interesting.
Well, I do not really have more to add. The only thing to be afraid of
is the first call. (That is two threads calling the same function for
the first time at about the same moment). But even if it sounds like an
unrealistic, it is not. My experience tells me that those problem will
occur if you do not take appropriate guards.
In this case I prefer a plain global variable - or a "real" function
that serialises the access in case there is some dependency you can not
get rid of (as you surely know, the order of construction within one
file is well-defined).
So put shortly: don't do what Frederick suggests unless you have given
it good thought.
Well, name some idiom in C++ for which that does not hold :-)
Surely. And this probably holds for any language.
/Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steven T. Hatton |
last post by:
If a header is not necessarily a source file, and the sequences delimited by
< and > in header names aren't necessarily valid source file names, what
exactly is a header?
--
p->m == (*p).m == p.m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
|
by: matthurne |
last post by:
I just started learning C++ on my own...I'm using Accelerated C++.
Something it hasn't explained and I keep wondering about is how header
files actually work. I suspect it doesn't get into it because it is,
as the authors love to say, "implementation specific". If that's the
case, how does the compiler commonly handle them? I use Linux and gcc
specifically. Basically, I don't understand how a header file being
included makes a...
|
by: ambika |
last post by:
Iam just trying to know "c".
And I have a small doubt about these header files.
The header files just contain the declaration part...Where is the
definition for these declarations written??And how does that get
linked to our program when we run it??I would appreciate any helpful
info..And I would like to thank you for that in advance
-ambika
|
by: C. Jayachandran |
last post by:
I've inherited some code which has const std::string values defined in
a header file, like
const std::string str = "foo";
This causes a large amount of bloat, as all the compilation units
including this header file will have a copy of the string, as well as
code to construct and destruct the string, even if the string is not
used within the CPP file.
|
by: Egbert Nierop |
last post by:
Hi,
Is it possible to redirect the linker & compiler to use a similar function
without getting complaints about 'function x already in blah.obj'?
Of course, I'm not talking about a runtime hook but about compilation time.
| |
by: zouyongbin |
last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this
should not appear in a header file:
int ix;
The inclusion of any of these definitions in two or more files of the
same program will result in a linker error complaining about multiple
definitions. So this kind of definition should be avoided as much as
possible. But as we know, the definition of a class is always in a
header file. And we can use "#ifndef" to eliminate...
|
by: Stephen Howe |
last post by:
Hi
Just going over some grey areas in my knowledge in C++:
1) If I have
const int SomeConst = 1;
in a header file, it is global, and it is included in multiple translations
units, but it is unused, I know that it does not take up storage in the
|
by: whirlwindkevin |
last post by:
I saw a program source code in which a variable is defined in a header
file and that header file is included in 2 different C files.When i
compile and link the files no error is being thrown.How is this
possible.I thought it will throw "Variable redefinition Error". Giving
the source code for reference.Please help me out on this...
a.h
-----
int g;
void fun(void);
|
by: bcpkh |
last post by:
Hello All
Received a header file from a supplier that defines an interface to
implement but it's giving me a problem, I reproduce the general
structure of the header file below;
#ifndef XYZ_H
various #define(s)
|
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: 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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |