473,322 Members | 1,232 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,322 software developers and data experts.

Instansing singleton right in the header file

I need to have one object for each template argument(s) used. For
example, I need to have one object of int. I tried the following code
and it gives me all I want with Visual C++ 7.1. But is it portable???
Will all compilers produce code that prints "single"?

Instancing of object right in header file (that can be include multiple
times - i.e. in multiple cpp files) give rise to my doubts...
Singleton.cpp
----------------------
#pragma once
template <typename T>
class Singleton
{
public:
static T * GetInstance()
{
static T Instance;
return &Instance;
}
};
First.cpp
----------------------
#include <iostream>
#include "Singleton.h"

void * First()
{
return Singleton<int>::GetInstance();
}

void * Second();

int main()
{
std::cout << (First() == Second() ? "single" : "multiple" );
std::cout << std::endl;
return 0;
}
Second.cpp
----------------------
#include <iostream>
#include "Singleton.h"

void * Second()
{
return Singleton<int>::GetInstance();
}

Mar 17 '06 #1
3 5698
Raider wrote:
I need to have one object for each template argument(s) used. For
example, I need to have one object of int. I tried the following code
and it gives me all I want with Visual C++ 7.1. But is it portable???
Of course.
Will all compilers produce code that prints "single"?
All compliant ones will.
Instancing of object right in header file (that can be include multiple
times - i.e. in multiple cpp files) give rise to my doubts...
You're not instantiating it "in header file". You're instantiating it
in a function. By the time the function is generated, compiled, and
then executed, there are no header files (reminds me of "there is no
spoon" from the Matrix movie).
Singleton.cpp
----------------------
#pragma once
template <typename T>
class Singleton
{
public:
static T * GetInstance()
{
static T Instance;
return &Instance;
}
};
First.cpp
----------------------
#include <iostream>
#include "Singleton.h"

void * First()
{
return Singleton<int>::GetInstance();
}

void * Second();

int main()
{
std::cout << (First() == Second() ? "single" : "multiple" );
std::cout << std::endl;
return 0;
}
Second.cpp
----------------------
#include <iostream>
#include "Singleton.h"

void * Second()
{
return Singleton<int>::GetInstance();
}


V
--
Please remove capital As from my address when replying by mail
Mar 17 '06 #2
Raider wrote:
I need to have one object for each template argument(s) used. For
example, I need to have one object of int. I tried the following code
and it gives me all I want with Visual C++ 7.1. But is it portable???
Will all compilers produce code that prints "single"?

Instancing of object right in header file (that can be include multiple
times - i.e. in multiple cpp files) give rise to my doubts...
Absent the export keyword (which itself is not very portable at the
moment), templates *must* be defined in the header file. The
compiler/linker will make sure there is actually only one instance in
the the final product.
Singleton.cpp
----------------------
#pragma once
Non-standard. Prefer #ifndef include guards.
template <typename T>
class Singleton
{
public:
static T * GetInstance()
{
static T Instance;
return &Instance;
}
};
You might consider returning a reference and disabling some other
functions just for safety:

private:
Singleton();
Singleton( const Singleton& );
Singleton& operator=( const Singleton& );
Singleton* operator&();
~Singleton();
First.cpp
----------------------
#include <iostream>
#include "Singleton.h"

void * First()
{
return Singleton<int>::GetInstance();
}

void * Second();

int main()
{
std::cout << (First() == Second() ? "single" : "multiple" );
std::cout << std::endl;
return 0;
}
Second.cpp
----------------------
#include <iostream>
Unnecessary here.
#include "Singleton.h"

void * Second()
{
return Singleton<int>::GetInstance();
}


Cheers! --M

Mar 17 '06 #3
Raider wrote:
I need to have one object for each template argument(s) used. For
example, I need to have one object of int. I tried the following code
and it gives me all I want with Visual C++ 7.1. But is it portable???
Will all compilers produce code that prints "single"?

Instancing of object right in header file (that can be include multiple
times - i.e. in multiple cpp files) give rise to my doubts...
Singleton.cpp
----------------------
#pragma once
template <typename T>
class Singleton
{
public:
static T * GetInstance()
{
static T Instance;
return &Instance;
}
};
First.cpp
----------------------
#include <iostream>
#include "Singleton.h"

void * First()
{
return Singleton<int>::GetInstance();
}

void * Second();

int main()
{
std::cout << (First() == Second() ? "single" : "multiple" );
std::cout << std::endl;
return 0;
}
Second.cpp
----------------------
#include <iostream>
#include "Singleton.h"

void * Second()
{
return Singleton<int>::GetInstance();
}


The code is not portable because it's using #pragma once instead of
standard header guards.
I'm guessing that Singleton.cpp is a typo, since it should be
Singleton.h

If you have a limitted set of types you want to use with your Singleton
template class, there is a way to put the implementation inside a *.cpp
file (even if you don't have export keyword support).
You can do template forward declaration.
Example:
//Your Singleton.cpp
template Singleton<int>;
template Singleton<std::string>;
#include "foofoo.h"
template Singleton<foofoo>;
//Your Singleton implementation here
If you have the above code in the Singleton.cpp file, than you can
create an instance of your Singleton template of type int, std::string,
or foofoo.
If you used a different type, than you would get a linker error.
This method is only usefull if you're only going to support limitted
types for your Singleton template.

Mar 17 '06 #4

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

Similar topics

9
by: tarmat | last post by:
I have a singleton class that looks a little like this: class MyClass { private: //data MyClass() {
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
6
by: Tony Johansson | last post by:
Hello! I have a class called Outdoors which is a singleton see below for definition and a main that call this instance method. Now to my problem this piece of code doesn't compile I get the...
5
by: LinuxGuy | last post by:
Hi, I have come across singleton class with some member variables are declared as static with public scope. As singleton class always return only one instance. ie. single copy of object is ...
3
by: Jeremy Cowles | last post by:
Will the keyword "Me" ever be able to be the target of an assignment? Because singleton classes would be MUCH simplier if you could just point Me to a different address. For example: Class...
5
by: Eric | last post by:
I am implementing a variation on the Singleton design pattern, that allows up to 8 objects of a class to be instantiated, and returns a null pointer for anything more than 8. I am running into a...
8
by: Frederick Gotham | last post by:
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....
5
by: Markus Dehmann | last post by:
I need a Singleton for general program options so that all classes can access it. I use the code below (adapted from the Wikipedia singleton example). But the problem is if I change one variable...
1
by: James Kanze | last post by:
On Apr 11, 12:09 am, Ron Eggler <t...@example.comwrote: That's usually a feature, not a bug, where singletons are concerned. In general, I avoid ever deleting a singleton. From the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.