I have the following code inside the .h file and I'm not sure what does the assignment statement do and how is it called properly?
我在.h文件中有以下代码,我不确定赋值语句的作用是什么以及如何正确调用它?
virtual void yield() = 0;
I thought that the function returns a value of 0 by default but since this function returns void I am a little bit confused. Can anyone comment on this and maybe say how can I refer to this assignment, I mean how is it called in C++ jargon?
我认为该函数默认返回值为0,但由于此函数返回void,我有点困惑。任何人都可以对此发表评论并且可能会说我怎么能参考这个赋值,我的意思是如何用C ++术语来调用它?
Thanks.
4 个解决方案
#1
22
This is a pure virtual function. This means, that subclasses have to implement this function, otherwise they are abstract, meaning you cannot create objects of that class.
这是一个纯虚函数。这意味着,子类必须实现此函数,否则它们是抽象的,这意味着您无法创建该类的对象。
class ISomeInterface {
public:
virtual std::string ToString( ) = 0;
}
class SomeInterfaceImpl : public ISomeInterface {
public:
virtual std::string ToString( ) {
return "SomeInterfaceImpl";
}
}
The idea is, that a class can expose a certain method, but subclasses have to implement it. In this example, ISomeInterface
exposes a ToString
method, but there is no sensible default implementation for that, so it makes the method pure virtual. Subclasses like SomeInterfaceImpl
can then provide a fitting implementation.
这个想法是,类可以暴露某个方法,但是子类必须实现它。在此示例中,ISomeInterface公开了一个ToString方法,但没有合理的默认实现,因此它使该方法纯虚拟。像SomeInterfaceImpl这样的子类可以提供适合的实现。
#2
9
The = 0
syntax declares a pure virtual function, it has nothing to do with the return value.
= 0语法声明了一个纯虚函数,它与返回值无关。
If a class contains at least one pure virtual function, that makes the class "abstract", which means it cannot be instantiated.
如果一个类包含至少一个纯虚函数,则使该类为“抽象”,这意味着它无法实例化。
In practice, such classes need to be concretized by subclassing and implementing the virtual function(s).
实际上,这些类需要通过子类化和实现虚函数来具体化。
#3
3
if is a pure virtual method (aka abstract) look here or google http://www.artima.com/cppsource/pure_virtual.html
如果是一个纯粹的虚拟方法(又名抽象),请看这里或谷歌http://www.artima.com/cppsource/pure_virtual.html
= 0 doesn't mean default return value, it is notification that function is pure virtual
= 0并不意味着默认返回值,它是通知功能是纯虚拟的
#4
0
The syntax is obscure, but the "=0" signifies the method is a pure virtual function. It makes the class abstract (you can't instantiate it) and it's implementation is left to derived classes.
语法模糊,但“= 0”表示该方法是纯虚函数。它使类抽象(你不能实例化它),它的实现留给派生类。
This is used when all you want to define is an interface. Use the virtual keyword when you want to define an interface and also provide a default implementation.
当您要定义的所有内容都是接口时使用。如果要定义接口,请使用virtual关键字,并提供默认实现。
#1
22
This is a pure virtual function. This means, that subclasses have to implement this function, otherwise they are abstract, meaning you cannot create objects of that class.
这是一个纯虚函数。这意味着,子类必须实现此函数,否则它们是抽象的,这意味着您无法创建该类的对象。
class ISomeInterface {
public:
virtual std::string ToString( ) = 0;
}
class SomeInterfaceImpl : public ISomeInterface {
public:
virtual std::string ToString( ) {
return "SomeInterfaceImpl";
}
}
The idea is, that a class can expose a certain method, but subclasses have to implement it. In this example, ISomeInterface
exposes a ToString
method, but there is no sensible default implementation for that, so it makes the method pure virtual. Subclasses like SomeInterfaceImpl
can then provide a fitting implementation.
这个想法是,类可以暴露某个方法,但是子类必须实现它。在此示例中,ISomeInterface公开了一个ToString方法,但没有合理的默认实现,因此它使该方法纯虚拟。像SomeInterfaceImpl这样的子类可以提供适合的实现。
#2
9
The = 0
syntax declares a pure virtual function, it has nothing to do with the return value.
= 0语法声明了一个纯虚函数,它与返回值无关。
If a class contains at least one pure virtual function, that makes the class "abstract", which means it cannot be instantiated.
如果一个类包含至少一个纯虚函数,则使该类为“抽象”,这意味着它无法实例化。
In practice, such classes need to be concretized by subclassing and implementing the virtual function(s).
实际上,这些类需要通过子类化和实现虚函数来具体化。
#3
3
if is a pure virtual method (aka abstract) look here or google http://www.artima.com/cppsource/pure_virtual.html
如果是一个纯粹的虚拟方法(又名抽象),请看这里或谷歌http://www.artima.com/cppsource/pure_virtual.html
= 0 doesn't mean default return value, it is notification that function is pure virtual
= 0并不意味着默认返回值,它是通知功能是纯虚拟的
#4
0
The syntax is obscure, but the "=0" signifies the method is a pure virtual function. It makes the class abstract (you can't instantiate it) and it's implementation is left to derived classes.
语法模糊,但“= 0”表示该方法是纯虚函数。它使类抽象(你不能实例化它),它的实现留给派生类。
This is used when all you want to define is an interface. Use the virtual keyword when you want to define an interface and also provide a default implementation.
当您要定义的所有内容都是接口时使用。如果要定义接口,请使用virtual关键字,并提供默认实现。