LINUX.ORG.RU

История изменений

Исправление i-rinat, (текущая версия) :

#include <boost/serialization/singleton.hpp>
#include <iostream>
using namespace std;
using namespace boost::serialization;

template <class T> class TServer :
    public singleton<TServer<T>>
{
private:
    int a;
    T b;
public:
    TServer() {
        // http://www.boost.org/doc/libs/1_48_0/libs/serialization/doc/singleton.html#requirements
        // In order to be used as singleton<T> , the type T must be default constructable.
        cout<<"TServer"<<endl;
    }
    int geta(){return a;};
    int getb(){return b;};
    void seta(int c) {a=c;}
    void setb(const T & c){b=c;};
};

TServer<int> &i() { return TServer<int>::get_mutable_instance(); }

int main(int argc, char**argv)
{
    i().seta(15);
    i().setb(-5);
    cout<<i().geta()<<endl;
    i().seta(25);
    cout<<i().getb()<<endl;
    cout<<i().getb()<<endl;
    cout<<i().geta()<<endl;
    cout<<i().geta()<<endl;
    i().seta(105);
    cout<<i().geta()<<endl;
    cout<<i().geta()<<endl;
    cout<<i().geta()<<endl;
    return 0;
}

Вывод:

TServer
15
-5
-5
25
25
105
105
105

Исходная версия i-rinat, :

#include <boost/serialization/singleton.hpp>
#include <iostream>
using namespace std;
using namespace boost::serialization;

template <class T> class TServer :
    public singleton<TServer<T>>
{
private:
    int a;
    T b;
public:
    TServer() {
        // http://www.boost.org/doc/libs/1_48_0/libs/serialization/doc/singleton.html#requirements
        // In order to be used as singleton<T> , the type T must be default constructable.
        cout<<"TServer"<<endl;
    }
    int geta(){return a;};
    int getb(){return b;};
    void seta(int c) {a=c;}
    void setb(const T & c){b=c;};
};

TServer<int> &i() { return TServer<int>::get_mutable_instance(); }

int main(int argc, char**argv)
{
    i().seta(15);
    i().setb(-5);
    cout<<i().geta()<<endl;
    i().seta(25);
    cout<<i().getb()<<endl;
    cout<<i().getb()<<endl;
    cout<<i().geta()<<endl;
    cout<<i().geta()<<endl;
    i().seta(105);
    cout<<i().geta()<<endl;
    cout<<i().geta()<<endl;
    cout<<i().geta()<<endl;
    return 0;
}