纯虚拟和虚拟有什么区别

发布于 2024-10-13 07:37:20 字数 152 浏览 7 评论 0原文

我正在修改祖父类是纯虚拟的代码,其中包括函数 XYZ 的纯虚拟版本;然后父类将 XYS 声明为虚拟的,并且它有一个实现。然后子类将 XYZ 声明为常规函数,其实现与父类 1 9 的实现不同,这本身就让我感到困惑)。当我从另一个对象调用函数 XYZ 时,会执行哪个实现?父母一还是孩子一?谢谢

I am modifying code that the grand-parent class is a pure virtual which include a pure virtual version of function XYZ ; the then parent class declares XYS as virtual and it has an implementaion for it. Then the child class declares XYZ as a regular function with a different implementation from that of the parent1 9which in itself is confusing to me). When I call the function XYZ from another object, which implementation gets executed? the parent one or the child one? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

谢绝鈎搭 2024-10-20 07:37:20

当我从另一个对象调用函数 XYZ 时,会执行哪个实现?父级还是子级?

让我们看看:

struct A {
  virtual void f() = 0;
};

struct B : A {
  virtual void f() { cout << "B::f\n"; }
};

struct C : B {
  virtual void f() { cout << "C::f\n"; }
};

int main() {
  C().f();
  return 0;
}

When I call the function XYZ from another object, which implementation gets executed? the parent one or the child one?

Let's find out:

struct A {
  virtual void f() = 0;
};

struct B : A {
  virtual void f() { cout << "B::f\n"; }
};

struct C : B {
  virtual void f() { cout << "C::f\n"; }
};

int main() {
  C().f();
  return 0;
}
兲鉂ぱ嘚淚 2024-10-20 07:37:20

纯虚函数不需要定义(与虚函数相反),并且它们使类抽象。抽象类不能创建对象,除非它们充当基类对象。

您的困惑似乎集中在 virtual 关键字的存在或不存在上。如果一个函数在基类中被声明为virtual,那么派生类的函数,无论是否添加virtual关键字,都会自动变为virtual< /code> 如果它具有相同的名称、参数类型和常量。

因此,如果您在实际指向 child 对象的 grandparent*parent* 上调用 XYZ,则 child 的 XYZ 对象将被执行。

Pure virtual functions don't need a definition (as opposed to virtual functions), and they make their class abstract. Abstract classes can't have an object created from, except when they act as base class object.

Your confusion seems to center around the presence or absence of the virtual keyword. If a function is declared virtual in a base class, then a function of a derived class, whether or not you put a virtual keyword, will automatically become virtual if it has the same name, parameter types and constness.

So if you call XYZ on a grandparent* or parent* that actually points to a child object, then the XYZ of the child object will be executed.

独自←快乐 2024-10-20 07:37:20

纯虚函数只是意味着没有纯虚函数的类可以被实例化。一旦父类重写了它,它就变成了一个普通的虚函数。一旦函数在基类中被声明为虚函数,它就始终是虚函数,无论继承类是否将其定义为虚函数。

编辑:这意味着调用它只是像其他任何函数一样调用虚拟函数 - 也就是说,将调用最派生类的实现。

A pure virtual function just means that no class with a pure virtual function can be instantiated. Once the parent class overrides it, it just becomes a normal virtual function. Once a function has been declared virtual in a base class, it is always virtual, regardless of whether or not the inheriting class defines it as virtual.

Edit: That means that calling it is just calling a virtual function like any other- that is, the most derived class's implementation will be called.

可可 2024-10-20 07:37:20

纯虚函数通常没有实现,并创建了类的“抽象性”。

编译器不会让您创建抽象类的实例。从此类派生的任何类都保持抽象,除非它继承的所有纯虚函数都已实现(并且不添加任何新函数)。这样的类称为具体类。

请注意,纯虚函数可能会被赋予一个实现(尽管由于语法原因无法内联)。另外,你可以有一个纯虚拟析构函数,然后必须给它一个实现(即使它是一个空的)。

您可以通过在末尾添加 =0 来指示纯虚函数:

virtual void foo(); // not pure
virtual void bar() = 0; // pure virtual

A pure virtual function usually has no implementation and creates the "abstractness" of the class.

The compiler will not let you create an instance of a class that is abstract. Any class that derives from this class remains abstract unless all the pure virtual functions it inherits have been implemented (and it doesn't add any new ones). Such a class is called concrete.

Note that a pure virtual function may be given an implementation (although it cannot be inlined for syntax reasons). In addition, you can have a pure virtual destructor, and then it must be given an implementation (even if it is an empty one).

You indicate a pure virtual function by adding =0 at the end thus:

virtual void foo(); // not pure
virtual void bar() = 0; // pure virtual
苏别ゝ 2024-10-20 07:37:20
class sample
{
public:
   virtual void fun(); //virtual function
   virtual void sun()=0; //pure virtual function
};

纯虚函数是通过分配 0 来声明的,如上所述。提供虚函数的定义是可选,而提供虚函数的定义是强制,否则不会编译。此外,您无法创建定义单个虚函数的类实例。

class sample
{
public:
   virtual void fun(); //virtual function
   virtual void sun()=0; //pure virtual function
};

Pure virtual function is declared by assigning 0, as done above. Providing definition for pure virtual functions is optional, while providing definition for virtual functions is compulsory, otherwise it will not compile. Also, you cannot create instance of a class that defines even a single pure virtual function.

顾挽 2024-10-20 07:37:20

虚函数是可以在派生类中重写的函数。

纯虚函数是根本没有实现的函数,因此必须在派生类中被重写。

除非您使用自己的实现覆盖任何纯虚函数,否则无法创建具有纯虚函数的类的实例或派生自该类的类的实例。

A virtual function is one that can be overridden in a derived class.

A pure virtual function is one that has no implementation at all and therefore MUST be overridden in a derived class.

You cannot create an instance of a class with a pure virtual function, or a class that derives from it unless you override any pure virtual functions with your own implementation.

天赋异禀 2024-10-20 07:37:20

对于虚函数,如果您在子类的对象上调用 xyz,始终 xyz 的子版本 (Child::xyz()) 将是被执行。

非虚拟重写方法不会出现这种情况。然后,如果您有一个指针 Parent* ptr = &child,则 ptr>xyz() 实际上会执行 Parent::xyz()。但就您而言,它是 Child::xyz()。这就是您使用 virtual 关键字的原因。

In the case of a virtual function, if you call xyz on an object of a child class, always the child version of xyz (Child::xyz()) will be executed.

It would not be a case with non-virtual overridden methods. Then if you had a pointer Parent* ptr = &child, ptr>xyz() would actually execute Parent::xyz(). But in your case it is Child::xyz(). That is why you use the virtual keyword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文