npr.
Imas uredjaje a (logicki)mis je jedinstven.
To sto sam ja kacio i po tri ( fizicka ) komada pa opet imam jednu strelicu.
U C++ ide otprilike ovako:
Code:
//: C10:Selfmem.cpp
// Static member of same type
// ensures only one object of this type exists.
// Also referred to as a "singleton" pattern.
#include <iostream>
using namespace std;
class Egg
{
static Egg e;
int i;
Egg(int ii) : i(ii) {}
public:
static Egg* instance() { return &e; }
int val() { return i; }
};
Egg Egg::e(47);
int main() {
//! Egg x(1); // Error -- can't create an Egg
// You can access the single instance:
cout << Egg::instance()->val() << endl;
} ///:~
Inace gornji kod je copy/paste iz Bjarnove knjige Thinking in C++ 2nd edition.
Takodje ima i u knjizi Modern C++ Design: Generic Programming and Design Patterns Applied od Andrei Alexandrescu.
[Ovu poruku je menjao 1jedini dana 29.11.2006. u 11:14 GMT+1]