如何使用C Sharp在派生类中使用基类的构造函数
how to use constructors of base class in derived class using c sharp
我有一个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
相关文章:
- C++17 使用驱动类常量作为基类构造函数的参数来初始化基类构造函数
- 派生类(构造函数具有参数)和基类(构造函数缺少参数)之间没有可行的转换
- 在成员构造函数之后调用基类构造函数
- 如何在将原始指针移动到基类构造函数之前从unique_ptr中提取原始指针
- C++:在共享对象中调用抽象基类构造函数/未定义的符号
- 如何在 C++ 中将参数传递给基类构造函数
- 将数据成员的指针传递给基类构造函数是否安全?
- 使用基类构造函数初始化儿童类
- 使用基类构造函数的子类构造函数的大纲定义
- MSVC 无法识别继承模板类的模板类的"直接"基类构造函数
- 子类的构造函数后跟冒号后的基类构造函数是什么意思?
- 是否可以使用基类构造函数通过派生类将值初始化为基类私有成员?
- 当派生类无法轻易将参数传递到基类时,如何调用基类构造函数
- 是否可以在运行时切换到不同的基类构造函数?
- 对基类构造函数的未定义引用
- 如何为基类构造函数中的每个子类执行特定任务
- 将类传递到基类构造函数的模板参数中?
- 如何在基类构造函数中使用派生类成员
- (为什么)纯虚拟派生类中是否需要虚拟基类构造函数调用?
- 使用相同的参数调用基类C++构造函数