簡體   English   中英

C ++到C#移植

[英]C++ to C# porting

如何將此代碼(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() { }
};

假設它真的是一個純粹的抽象基類,它看起來像:

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

但傳遞約定的確切參數是尷尬的。 如果不知道你想要做什么,很難確切地說你應該在C#中做些什么。 可能, void FunctionName(ref T arg)可能更合適。

有些事情:

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);
    }

這似乎有效:D

你可以寫這樣的

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();
}

修復你的T到EntityType類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM