Read only erwünscht!



  • Folgendes Problem:

    Header:

    #ifndef included_random_h
    #define included_random_h
    
    #include "square.h"
    
    extern uint64 random_white_pawn[SQUARES];
    extern uint64 random_white_knight[SQUARES];
    extern uint64 random_white_bishop[SQUARES];
    extern uint64 random_white_rook[SQUARES];
    extern uint64 random_white_queen[SQUARES];
    extern uint64 random_white_king[SQUARES];
    
    // much more...
    
    extern void random_initialize(void);
    
    #endif
    

    Die Arrays werden in einem source-code File initialisiert und *sollen
    *anschliessend nur noch read only sein.

    Kann natürlich in der Header schreiben:

    inline uint64 random_get_white_pawn(const sintx square) {
      return random_white_pawn[square];
    }
    

    Ist aber nicht wirklich gut, da die Arrays immer noch frei herumklaufen.

    Schlechte Lösung:

    Ich baue eine Klasse:

    #ifndef included_random_h
    #define included_random_h
    
    #include "square.h"
    
    class Random {
    private:
      static uint64 random_white_pawn[SQUARES];
      static uint64 random_white_knight[SQUARES];
      static uint64 random_white_bishop[SQUARES];
      static uint64 random_white_rook[SQUARES];
      static uint64 random_white_queen[SQUARES];
      static uint64 random_white_king[SQUARES];
      // much more...
    
    public: 
      static void random_initialize(void);
      static uint64 random_get_white_pawn(const sintx square);
      // much more...
    };
    
    inline uint64 Random::random_get_white_pawn(const sintx square) {
      return random_white_pawn[square];
    }
    
    // much more...
    
    #endif
    

    Da mal wieder alles static ist und es keine Instanzen dieser read-only Klasse gibt, ist das auch nicht das gelbe vom Ei.

    Was habe ich für eine Möglichkeit einen sauberen read-only Mechanismus zu erzwingen?

    Danke!

    🙄



  • vielleicht sowas:

    #include <iostream>
    using namespace std;
    
    int const SQUARES=4;
    
    int const (&init_rwp())[SQUARES]{
    	static int feld[SQUARES];
    	feld[0]=47;
    	feld[1]=11;
    	feld[2]=1;
    	feld[3]=2;
    	return feld;
    }
    
    int const (&random_white_pawn)[SQUARES]=init_rwp();
    
    int main(){
    	cout<<random_white_pawn[1]<<'\n';
    	cout<<sizeof(random_white_pawn)<<'\n';
    //	random_white_pawn[1]=2;//geht nicht
    }
    


  • Ich lege vielleicht falsch, aber sowas Einfaches:

    extern const uint64 random_white_pawn[SQUARES];
    extern const uint64 random_white_knight[SQUARES];
    extern const uint64 random_white_bishop[SQUARES];
    extern const uint64 random_white_rook[SQUARES];
    extern const uint64 random_white_queen[SQUARES];
    extern const uint64 random_white_king[SQUARES];
    

    Andere Datei:

    const uint64 random_white_pawn[SQUARES] = {...};
    ...
    

    funktioniert nicht?



  • CppCoder schrieb:

    Ich lege vielleicht falsch, aber sowas Einfaches:

    extern const uint64 random_white_pawn[SQUARES];
    extern const uint64 random_white_knight[SQUARES];
    extern const uint64 random_white_bishop[SQUARES];
    extern const uint64 random_white_rook[SQUARES];
    extern const uint64 random_white_queen[SQUARES];
    extern const uint64 random_white_king[SQUARES];
    

    Andere Datei:

    const uint64 random_white_pawn[SQUARES] = {...};
    ...
    

    funktioniert nicht?

    Doch, geht.
    Ich ging davon aus, daß eine Funktion das Initialisieren machen muß.
    Aber wenn ich's recht bedenke, werden das wohl die magic numbers für's zobrist hashing, die können gern fertig vorausberechnet sein.



  • [cpp]
    class MyClass
    {
    public:
    static MyClass& GetInstance() const
    {
    return m_instance;
    }

    const uint64 GetRandomWhitePawn() const
    {
    return random_white_pawn [SQUARES];
    }

    private:
    MyClass m_instance;
    uint64 random_white_pawn[SQUARES];
    MyClass(){}
    };
    [/const]
    ?



  • daß eine Funktion das Initialisieren machen muß.

    Kann es vielleicht eine template-basierende Compile-time Funktion sein?
    Das liegt ganz klar an Schwierigkeitsgrad der Funktion selbst.

    PS In neuem C++0x Standard kann man eine Funktion als constexpr definieren. dann wird so was:

    const uint64 myvar = funcA()
    

    auch möglich sein.


Anmelden zum Antworten