Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

21

13.08.2007, 21:39

Hmm ... nochmal zu deinem Ansatz ... sieht ganz gut aus, nur ist das Problem, wie mir scheint, das base und derived nicht direkt verknüpft sind bzw. die zwar von der gleichen Klasse erben, wenn ich aber den Base-Pointer nutze, ich nicht das derived objekt erhalte ;)

C-/C++-Quelltext

1
2
3
4
5
6
7
8
inline static base* instance(base* ptr = NULL) 
{
    static unsigned short count = 0;
    std::assert(count++ != 0);
    static base* _inst = ptr; 
    if (_inst == NULL) throw std::runtime_error("no instance"); 
    return _inst; 
}
wäre schon eher was ... jetzt hab ich nur das Problem, dass das auch nicht verhindert, das eine 2. instanz erzeugt wird ... kann zwar der Klasse nicht übergeben werden ... aber trotzdem ...
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

22

14.08.2007, 09:18

Was meinst du?

C-/C++-Quelltext

1
base* a = derived::create();


Geht doch ganz hervorragend!

Nochmal im Ganzen:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <map>
#include <cassert>

struct uncopyable
{
protected:
    uncopyable() {}
    ~uncopyable() {}

private:
    uncopyable( const uncopyable& );
    uncopyable& operator=( const uncopyable& );
};

struct base : protected uncopyable
{
    typedef void ( base::*memfun_ptr )( const void* );

    typedef std::map< std::size_t, memfun_ptr > list_type;
    typedef list_type::iterator iterator_type;

    static base* create()
    {
        static int instances = 0;

        assert( !( instances++ ) && "zu viele instanzen" );

        static base inst;
        return &inst;
    }
    
    void register_msg( std::size_t id, memfun_ptr handler )
    {
        list[ id ] = handler;
    }

    void call_msg( std::size_t id, const void* data )
    {
        iterator_type it( list.find( id ) );

        if ( it != list.end() )
            ( this->*( it->second ) )( data );
    }

    virtual void test( const void* data )
    {
        std::cout << "calling base::test()\n";
    }

protected:
    base()
    {
        register_msg( 0, &base::test );
    }

private:
    std::map< std::size_t, memfun_ptr > list;
};

struct derived : base
{
    static derived* create()
    {
        static int instances = 0;

        assert( !( instances++ ) && "zu viele instanzen" );

        static derived inst;
        return &inst;
    }

    void test( const void* data )
    {
        std::cout << "calling derived::test()\n";
    }

    void foo( const void* data )
    {
        std::cout << "calling derived::foo()\n";
    }

protected:
    derived()
    {
        register_msg( 0, static_cast< base::memfun_ptr >( &derived::test ) );
        register_msg( 1, static_cast< base::memfun_ptr >( &derived::foo ) );
    }
};

int main()
{
    base* a = derived::create(); // create derived object

    base* b = base::create();    // create base object

    //derived* c = derived::create(); // runtime assertion

    //base* d = base::create(); // runtime assertion


    a->call_msg( 0, 0 );    // calling derived::test

    a->call_msg( 1, 0 );    // calling derived::foo

    b->call_msg( 0, 0 );    // calling  base::test

}
@D13_Dreinig

23

14.08.2007, 15:35

hmm ok ... wenn man sich dann noch ne Funktion instance schreibt ... sollte es gehen ... kann es nämlich nicht gebrauchen den Zeiger immer rumreichen zu müssen ...
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

24

14.08.2007, 15:46

Naja, dass is ja weniger das Problem.

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
struct base : protected uncopyable
{
    static base* instance()
    {
        static base* inst = 0;

        if ( inst == 0 )
            inst = base::create();

        return inst;
    }

protected:
    static base* create()
    {
        static int instances = 0;

        assert( !( instances++ ) && "zu viele instanzen" );

        static base inst;
        return &inst;
    }

private:
    std::map< std::size_t, memfun_ptr > list;
};

struct derived : base
{
    static base* instance()
    {
        static base* inst = 0;

        if ( inst == 0 )
            inst = derived::create();

        return inst;
    }

protected:
    static derived* create()
    {
        static int instances = 0;

        assert( !( instances++ ) && "zu viele instanzen" );

        static derived inst;
        return &inst;
    }
};


Und schon läufts:

C-/C++-Quelltext

1
2
3
4
5
6
int main()
{
    base::instance()->call_msg( 0, 0 );
    derived::instance()->call_msg( 1, 0 );
    derived::instance()->call_msg( 0, 0 );
}
@D13_Dreinig

Werbeanzeige