R
Raider
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();
}
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();
}