简体   繁体   English

C ++到C#移植

[英]C++ to C# porting

How can I port this code (c++) to c#? 如何将此代码(c ++)移植到c#?

template <class entity_type>
class State {
public:
    virtual void Enter(entity_type*) = 0;
    virtual void Execute(entity_type*) = 0;
    virtual void Exit(entity_type*) = 0;
    virtual ~State() { }
};

Assuming that really is a purely abstract base class, which is what it looks like: 假设它真的是一个纯粹的抽象基类,它看起来像:

interface State<T>
{
    void Enter(T arg);
    void Execute(T arg);
    void Exit(T arg);
};

The exact argument passing convention is awkward though. 但传递约定的确切参数是尴尬的。 Without knowing exactly what you want to do it is hard to say exactly what you should do in C#. 如果不知道你想要做什么,很难确切地说你应该在C#中做些什么。 Possibly, void FunctionName(ref T arg) might be more appropriate. 可能, void FunctionName(ref T arg)可能更合适。

Some thing of the sort: 有些事情:

interface State<T> : IDisposable
{
    void Enter(T t);
    void Execute(T t);
    void Exit(T t);
}
public abstract class State<entity_type>
    {
        public abstract void Enter(entity_type obj);
        public abstract void Execute(entity_type obj);
        public abstract void Exit(entity_type obj);
    }

This seems to work :D 这似乎有效:D

you can write like this 你可以写这样的

abstract class State<T> : IDisposable where T : EntityType
{
    public abstract void Enter(T t);
    public abstract void Execute(T t);
    public abstract void Exit(T t);

    public abstract void Dispose();
}

fix your T to EntityType class. 修复你的T到EntityType类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM