473,396 Members | 1,847 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.

A question regarding to the keyword static

I just noticed an interesting implementation of Singleton from Bruce
Eckel's "Thinking in C++" vol. 2, pp 620:
class Singleton {
static Singleton s;
public:
static Singleton& instance() {
return s;
}
// ....
};
Singleton Singleton::s;

It is interesting to note that a static member of itself (Singleton)
is declared inside the Singleton class. I tested the code in linux,
and g++ compiled it successfully.

My question is: How could this happen? How could a class ever have a
member of the type of itself? This is obviously invalid if there is no
keyword static. However, what features of the keyword "static" make
this feasible?

Thanks for any comments or inputs.

Mar 22 '07 #1
1 1568

wizwx schrieb:
I just noticed an interesting implementation of Singleton from Bruce
Eckel's "Thinking in C++" vol. 2, pp 620:
class Singleton {
static Singleton s;
public:
static Singleton& instance() {
return s;
}
// ....
};
Singleton Singleton::s;

It is interesting to note that a static member of itself (Singleton)
is declared inside the Singleton class. I tested the code in linux,
and g++ compiled it successfully.

My question is: How could this happen? How could a class ever have a
member of the type of itself? This is obviously invalid if there is no
keyword static. However, what features of the keyword "static" make
this feasible?

Thanks for any comments or inputs.
A static class is only instanciated once. A normal member would result
a infinite class size and recursive constructor call.

Mar 22 '07 #2

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

Similar topics

9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
16
by: vaughn | last post by:
What is the 'this' keyeword for? If I'm filling a textbox, what's the difference between this.textbox1.Text = "my text"; and textbox1.Text = "my text"; ? I normally use it w/o the 'this'. Thanks.
14
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
3
by: Danny Din | last post by:
Hi, I have the following code – public class Class1 { public Class1()
54
by: Sahil Malik [MVP] | last post by:
What the heck - I can't find it. A bit shocked to see it missing though. So "Does VB.NET have the yield keyword, or any equivalent of it" ? -- - Sahil Malik Upcoming ADO.NET 2.0 book -...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
4
by: wizwx | last post by:
The followings are a few lines of code from the Template method in "Thinking in C++" vol.2 pp.639 class ApplicationFramework { protected: virtual void customize1() = 0; virtual void...
2
by: DaTurk | last post by:
Hi, I have an interesting issue, well, it's not really an issue, but I'd like to understand the mechanics of what's going on. I have a file, in CLI, which has a class declared, and a static...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
tracyyun
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...
0
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,...

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.