纯虚函数可以有参数吗?
can a pure virtual function has parameters?
如果我有一个 classes "Card" (Base Class) "CardOfType1" (Derived Class) 和一个 class "Player" 具有指向 'CardOfType1' 的 'Card' 类型的指针。有没有可能我们有一个名为 'playCard(Player enemyPlayer)'?
的纯虚函数
为了更加理解,下面给出代码
class Card
{
public:
virtual void playCard(Player enemyPlayer) = 0;
};
class CardOfType1
{
public:
void playCard(Player enemyPlayer)
{
//Some Code Goes here
}
};
class Player
{
stack<Card *> deckOfCards
//.
//.
//.
};
是的,PVF
可以有参数。
virtual void playCard(Player enemyPlayer) = 0;
这里 = 0
(不是赋值),我们只是通知编译器函数将是 pure
并且没有任何主体(在它声明的地方,在那个 class ), 但它可以有参数。
来自 n4659 C++
标准
A pure virtual function need be defined only if called with, or as if
with (15.4), the qualified-id syntax (8.1).
class shape {
point center;
public:
virtual void rotate(int) = 0; // pure virtual
virtual void draw() = 0; // pure virtual
};
但还有一个观察
A function declaration cannot provide both a pure-specifier and a
definition — end note ]
struct C {
virtual void f() = 0 { };
};
如果我有一个 classes "Card" (Base Class) "CardOfType1" (Derived Class) 和一个 class "Player" 具有指向 'CardOfType1' 的 'Card' 类型的指针。有没有可能我们有一个名为 'playCard(Player enemyPlayer)'?
的纯虚函数为了更加理解,下面给出代码
class Card
{
public:
virtual void playCard(Player enemyPlayer) = 0;
};
class CardOfType1
{
public:
void playCard(Player enemyPlayer)
{
//Some Code Goes here
}
};
class Player
{
stack<Card *> deckOfCards
//.
//.
//.
};
是的,PVF
可以有参数。
virtual void playCard(Player enemyPlayer) = 0;
这里 = 0
(不是赋值),我们只是通知编译器函数将是 pure
并且没有任何主体(在它声明的地方,在那个 class ), 但它可以有参数。
来自 n4659 C++
标准
A pure virtual function need be defined only if called with, or as if with (15.4), the qualified-id syntax (8.1).
class shape {
point center;
public:
virtual void rotate(int) = 0; // pure virtual
virtual void draw() = 0; // pure virtual
};
但还有一个观察
A function declaration cannot provide both a pure-specifier and a definition — end note ]
struct C {
virtual void f() = 0 { };
};