将C ++类转换为C#类

时间:2015-03-10 04:09:13

标签: c# c++

我有一个奇怪的C ++类,如下所示。我想在C#中写出这个类的等价,但是我遇到了麻烦。

class Map
{  
public:

    class Cell
    {
        public:

            class Hash : public unary_function<Cell*, size_t>
            {
                public:

                    static const int C;
                    size_t operator()(Cell* c) const;
            };

            static const unsigned int NUM_NBRS;
            static const double COST_UNWALKABLE;
            double cost;
            Cell(unsigned int x, unsigned int y, double cost = 1.0);
            ~Cell();
            void init(Cell** nbrs);
            Cell** nbrs();
            unsigned int x();
            unsigned int y();

        protected:
            bool _init;
            Cell** _nbrs;
            unsigned int _x;
            unsigned int _y;
    };

    Map(unsigned int rows, unsigned int cols);
    ~Map();
    Cell* operator()(const unsigned int row, const unsigned int col);
    unsigned int cols();
    bool has(unsigned int row, unsigned int col);
    unsigned int rows();

protected:

    Cell*** _cells;
    unsigned int _cols;
    unsigned int _rows;
};  

如何将其转换为C#?特别是我混淆了继承unary_function

的Hash类

1 个答案:

答案 0 :(得分:0)

它不起作用,你最好只使用c ++代码作为需求的描述。

如果那不是一个选项,那么我只能提供以下提示:

// have a create function for you Hash object (I'm trying to simplify here)
public class Hash
{
  static const int C= 1234;
  public static Func<Cell, int> CreateHashObject()
  {
    Func<Cell, int> hashObj= (cl)=>{return cl.GetHashCode() + C;};  // just a dummy implementation to show that you can save a copy of C in the created object.
    return hashObj;
  }
}

// and then wherever you need to create a Hash object
var myHashObject= Hash.CreateHashObject();