附加'= x'在 c++ 中的方法声明之后
在 C++ 中,当声明一个方法时,我注意到有时该方法可能会附加一个赋值。
谁能告诉我这是什么?
例如:
virtual void MyMethod () = 0;
“= 0”是什么意思? :)
谢谢大家!
In C++, when a method is declared, I've noticed that sometime the method may have an assignement appended to it.
Could anyone tell me what this is?
For example:
virtual void MyMethod () = 0;
What doe the '= 0' mean. :)
Thanks everyone !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这意味着它是一个纯虚函数,即该类中没有可用的实际定义,并且必须在子类中重写它。它实际上并不是一个分配,零是您可以“分配”的唯一值。
这是 C++ 语法;在 C# 中,使用
abstract
关键字可以完成相同的任务。It means it's a pure virtual function, i.e. no actual definition of it is available in this class and it must be overridden in a subclass. It's not actually an assignment as such, zero is the only value you can "assign".
And this is C++ syntax; in C# the same would be accomplished with the
abstract
keyword.在 C++ 中,这意味着该方法是纯虚方法。
这意味着无法实例化该特定类类型的实例。您只能创建从此派生的类的实例,这些实例将覆盖基类中的所有纯虚方法。
具有纯虚方法的基类定义了派生类必须实现的接口,并且并不意味着可以单独使用。
与 calmh 声称的相反,据我所知,可以实现纯虚函数并且可以显式调用它们。
不过,通常人们不会这样做,除非虚拟析构函数在基类中是纯的(在这种情况下必须定义它)。
In C++ this means that the method is a pure virtual method.
This means that an instance of this particular class-type cannot be instantiated. You can only create instances of classes derived from this, which override all pure virtual methods in the base class.
A base class with pure virtual methods defines an interface that derived classes have to implement, and is not meant to be used on its own.
Contrary to what calmh claims, as far as I know pure virtual functions can be implemented and they can be called explicitly.
Usually one wouldn't do this, though, except perhaps if the virtual destructor is pure in a base class (in this case it has to be defined).
在 C# 中,这是一个语法错误。
如果你指的是C++,请参阅calmh的回答。
In C#, that is a syntax error.
If you meant C++, see calmh's answer.