[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] 16 class'es



> Hvordan laver jeg objekter ud fra en variabel 0<=d<16, sålede at når d=0

Jeg har lige fornøjet mig med en implementering af ovenstaaende. Det blev maaske lidt uskønt
at se paa, men det har kompleksitet O(1). Man kan sikkert gøre det skønnere ved ikke at bruge
all det der template fis.

Ideen er at registrere et factory objekt for hver klasse N i et array ved indeks N. Instantiering
af klasse N foregaar saa ved at slaa factory objektet op ved indeks N i arrayet og bede factory
objektet om at faa en ny instans af klasse N.

God fornøjelse,
HC
----------------------------------------------------
// Eksempel paa instantiering af nummererede klasser med fælles superklasse og identiske constructors
// (C) 2006 Hans-Christian Stadler
#include <cassert>
#include <iostream>
#include <sstream>

using namespace std;

namespace {
        const int num_classes = 2;      // saa mange forskellige target(=dem er vi interesseret i) klasser

        class Obj {                     // fælles superklasse for vores target klasser
        public:
                virtual ~Obj () {}
                virtual ostream& msg (ostream&) const = 0; // print mit klassenavn
        };

        class Factory {                 // fælles superklasse for factory klasser af target klasserne
        protected:
                static Factory* class_factories[num_classes]; // array med factory klasserne af target klasserne

                virtual ~Factory () {}

        public:
                Obj* operator() (int cnum) const // instantier target klasse nummer <cnum>
                {
                        assert((cnum>=0) && (cnum<num_classes) && class_factories[cnum]);
                        return class_factories[cnum]->new_instance();
                }

                virtual Obj* new_instance () const { assert(0); } // instantier min target klasse

                static const Factory singleton; // indgang til factory systemet
        };

        Factory* Factory::class_factories[num_classes];
        const Factory Factory::singleton;

        template <class C, int N>
        class ClassFactory : public Factory { // jeg er i stand til at instantiere target klasse <C> med nummer <N>
                static const ClassFactory singleton;

                ClassFactory () // set mig selv ind det rigtige sted i array'et af klassefactories
                {
                        assert((N>=0) && (N<num_classes) && !class_factories[N]);
                        class_factories[N] = this;
                }

                virtual ~ClassFactory () {}

        public:
                Obj* new_instance () const { return new C; } // returner ny instans af target klasse C
        };

        template <class C, int N>
        const ClassFactory<C,N> ClassFactory<C,N>::singleton;

        template <int N>
        class C : public Obj { // jeg er target klasse C<N>, kun lavet med template for at spare tastetid
        public:
                virtual ~C () {}
                virtual ostream& msg (ostream &out) const { out << "C<" << N << '>'; return out; } // print mit klassenavn
        };

        ostream& operator<< (ostream &lhs, const Obj *rhs) { return rhs->msg(lhs); } // global << operator for C++ printning af Obj*

        template class ClassFactory<C<0>,0>; // eksplicit instantiering af factory klassen for C<0> som klasse nummer 0
        template class ClassFactory<C<1>,1>; // ditto for C<1> som klasse nummer 1
}

int main (int argc, char *argv[])
{
        int class_number;
        const Factory &factory = Factory::singleton; // indgang til factory systemet

        if (argc < 2)
                cerr << "Usage: " << argv[0] << " <class number in [0..1]>" << endl, exit(-1);

        {
                istringstream in(argv[1]);
                in >> class_number;
                if (! in)
                        cerr << "Error: Cannot parse class number" << endl, exit(-2);
                if ((class_number<0) || (class_number>=num_classes))
                        cerr << "Error: Invalid class number" << endl, exit(-3);
                cout << "Class number: " << class_number << endl;
        }

        Obj *obj = factory(class_number); // instantier klasse nummer <class_number>
        cout << "Created " << obj << endl; // print klassenavn af netop instantieret klasse
        delete obj;
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-05-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *