473,770 Members | 6,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

private static member initialization

Hello.

This (example) code will compile correctly with gcc <= 3.3.0, but doesn't
compile with gcc 3.3.1 (under linux):

-------------------------------------------------------------
template < typename T >
class Singleton
**{
**private:
****Singleton() *{}
****static*Sing leton*instance;

**public:
****static*Sing leton&*getInsta nce()*{*return* instance;*}
**};

template < typename T > Singleton<T> Singleton<T>::i nstance;

Singleton<int>& a = Singleton<int>: :getInstance();
--------------------------------------------------------------

This is the error returned by gcc:

test.cc: In instantiation of `Singleton<int> Singleton<int>: :instance':
test.cc:9:***in stantiated*from *`static*Single ton<T>&*Singlet on<T>::getInsta nce()*[with*T*=*int]'
test.cc:15:***i nstantiated*fro m*here
test.cc:5: error: `Singleton<T>:: Singleton() [with T = int]' is private
test.cc:12: error: within this context

Btw, the code also works with the "Test Drive Comeau C++ Online"...

Did I make some errors?
Is correct to initialize private data members in that manner?
(With a non-template "Singleton" no errors were produced)

I wish to test the code with the new gcc 3.3.2, but I have no ways to
install it by now...

Can you give me some suggestions?

Thank you in advance.
Francesco.
Jul 22 '05 #1
1 2392
"Francesco Monica" <fm*****@ce.uni pr.it> wrote...
This (example) code will compile correctly with gcc <= 3.3.0, but doesn't
compile with gcc 3.3.1 (under linux):

-------------------------------------------------------------
template < typename T >
class Singleton
{
private:
Singleton() {}
static Singleton instance;

public:
static Singleton& getInstance() { return instance; }
};

template < typename T > Singleton<T> Singleton<T>::i nstance;

Singleton<int>& a = Singleton<int>: :getInstance();
--------------------------------------------------------------

This is the error returned by gcc:

test.cc: In instantiation of `Singleton<int> Singleton<int>: :instance':
test.cc:9: instantiated from `static Singleton<T>& Singleton<T>::g etInstance() [with T = int]' test.cc:15: instantiated from here
test.cc:5: error: `Singleton<T>:: Singleton() [with T = int]' is private
test.cc:12: error: within this context

Btw, the code also works with the "Test Drive Comeau C++ Online"...

Did I make some errors?
Not that I can see.
Is correct to initialize private data members in that manner?
(With a non-template "Singleton" no errors were produced)

It's fine.
I wish to test the code with the new gcc 3.3.2, but I have no ways to
install it by now...
I believe some readers of this newsgroup can help, I am not one of
them, unfortunately. Have you tried gnu.g++.* newsgroups?

Can you give me some suggestions?


Contact the GNU GCC Project, they may have an idea. It does look
like a regression from the earlier version.

Victor
Jul 22 '05 #2

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

Similar topics

3
3614
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a DLL by another external object file (not within the DLL). It only occurs when the static const...
3
1710
by: Tran Tuan Anh | last post by:
Dear all, I am new with C++ and very confused with some features. Really appreciate if you can explain to me some of stuffs below. I define a class: class A { static A* instance = 0; };
3
6387
by: Mike - EMAIL IGNORED | last post by:
MyClass { //I have a static member method: void static myMethod(); //and a static data member: static MyType myData; }; //In the .cpp file: void MyClass::myMethod()
14
5934
by: Madhav | last post by:
hi all, I was trying the following code on the MS .net compiler: class Item { static int count; public: //static void init() { } Item()
8
8931
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
7
4033
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const member inside my class. class Packet { static const int session_id;
4
2729
by: Johs | last post by:
I have this in a file called bas.h: #ifndef BAS_H_ #define BAS_H_ class GeoObj { public: void draw() const; int getNum();
9
8895
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've *defined* "x"'s value to be 10, isn't above statement a
5
5621
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
15
7871
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
9591
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
9425
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,...
1
10001
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
9867
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 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...
0
8880
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...
0
5312
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
3969
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
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.