如何使用C Sharp在派生类中使用基类的构造函数

how to use constructors of base class in derived class using c sharp

本文关键字:基类 构造函数 何使用 Sharp 派生      更新时间:2023-10-16

我有一个C 代码,然后尝试将其转换为C尖锐代码

c 如下

class gate {
    friend class netlist;
    std::string type_, name_;
    virtual bool validate_structural_semantics();
protected:
    gate(std::string type, std::string name);
    ~gate();
}; // class gate
class netlist {
public:
    netlist();
    ~netlist();//destructor
}; // class netlist
class flip_flop: public gate
{
    bool state_, next_state_;
    bool validate_structural_semantics();
public:
    flip_flop(std::string name)
    : gate("dff", name), state_(false), next_state_(false) {}
}; // class flip_flop
bool flip_flop::validate_structural_semantics() 
{
    if (pins_.size() != 2) return false;
    pins_[0]->set_as_output();
    pins_[1]->set_as_input();
    return true;
}

c尖锐的代码我试图编写看起来像

    class Gate
    {
        public Gate(string name) { }
        public string type_, name_;
        public virtual bool Validate_structural_semantics()
        {
            // should be provided by derived classes                
        }
        public Gate(string type,string name) { }
    }; // class gate
    public class Netlist
    {
        public Netlist() { }
    }; // class netlist
    class Flip_flop : Gate
    {
        Flip_flop():base ("type","name") { }//issue is here cant use state_
        bool state_, next_state_;
        public Flip_flop(string name)//i see a red line under Flip_flop
        {
           this.name_ = name;
        }
        public override bool Validate_structural_semantics()
        {
             if (pins_.Count != 2) return false;
            pins_[0].Set_as_output();
            pins_[1].Set_as_input();
            return true;
        }
    }; // class flip_flop

如何根据我的代码来定义C Sharp的构造函数,该构建体会根据我的代码进行不同的论点?

问题是Gate没有默认构造函数,因此当您调用

public Flip_flop(string name)

它不知道如何构建基础。

尝试

public Flip_flop(string name) : base(name)

您在基类public Gate(string name)public Gate(string type,string name)中有两个构造,并且您想在Flip_flop类中使用这两个构造,因此您的类看起来像:

   class Flip_flop : Gate
    {
        bool state_, next_state_;
        public Flip_flop(string name) : base(name)
        {
            this.name_ = name;
        }
        public Flip_flop(string type, string name) : base(type, name)
        {
            this.name_ = name;
            state_ = false;
            next_state_ = false;
        }

        public override bool Validate_structural_semantics()
        {
            if (pins_.Count != 2) return false;
            pins_[0].Set_as_output();
            pins_[1].Set_as_input();
            return true;
        }
    }; // class flip_flop