简体   繁体   English

范围是否是访问虚拟功能的合法方式?

[英]Is scoping a legitimate way to access virtual functions?

I have a C++ program with 4 classes : Person, Student, Employee, and PartTimeStudent. 我有一个包含4个类的C ++程序:Person,Student,Employee和PartTimeStudent。

Student and Employee each derive from Person, and PartTimeStudent derives from all 3 classes (making it the most derived class). Student和Employee都派生自Person,而PartTimeStudent派生自所有3个类(使其成为派生程度最高的类)。 All classes have a virtual function called VDescribe(). 所有类都有一个名为VDescribe()的虚函数。

Please see the code below : 请参阅以下代码:

class Person
{
    ...    
    virtual void VDescribe();
    ...
};

class Student : virtual public Person
{
    ...    
    virtual void VDescribe();
    ...
};

class Employee : virtual public Person
{
    ...    
    virtual void VDescribe();
    ...
};

class PartTimeStudent : virtual public Person,
    virtual public Student,
    virtual public Employee
{
    ...    
    virtual void VDescribe();
    ...
};

Note : In the code snippet above I have omitted the Constructors, Destructors and member variables because they are not relevant to the question. 注意:在上面的代码片段中,我省略了构造函数,析构函数和成员变量,因为它们与问题无关。

In addition, I have the following code in which a PartTimeStudent object is created and accessed via a pointer. 另外,我有以下代码,其中通过指针创建和访问PartTimeStudent对象。 I use scoping to invoke the VDescribe() functions of the different sub-objects within the PartTimeStudent object. 我使用作用域来调用PartTimeStudent对象中不同子对象的VDescribe()函数。

void DoTest()
{
    PartTimeStudent* pPTS = new PartTimeStudent("John", 23, "NTU", "Seven-Eleven");

    pPTS->VDescribe();
    pPTS->::Person::VDescribe();
    pPTS->::Student::VDescribe();
    pPTS->::Employee::VDescribe();
}

The code compiles successfully and I am able to invoke the different versions of VDescribe(). 代码编译成功,我可以调用不同版本的VDescribe()。 What I want to know is, is this a legitimate means of accessing virtual functions? 我想知道的是,这是一种访问虚拟功能的合法手段吗? Is this acceptable or a discouraged practice? 这是可接受的还是气馁的做法?

Yes, this is a perfectly legal way of bypassing dynamic dispatch and calling a particular version of a virtual function, not its final overrider. 是的,这是绕过动态调度和调用虚拟函数的特定版本而不是最终覆盖的完全合法方式。

However, I would generally find it strange to see such code on the outside of the class, and I'd be checking to see if there's perhaps a design problem or misunderstanding. 但是,我通常会发现在课堂看到这样的代码很奇怪,我会检查是否存在设计问题或误解。 Normally, such code is used inside the class itself to call the overridden version of a function (typically from the overrider). 通常,此类代码在类本身内部用于调用函数的重写版本(通常来自overrider)。

It's a legitimate way to non-virtually call these functions. 是非虚拟调用这些函数的合法方式。 There is no general prohibition, but you have to be careful when doing this in constructors (as your base classes are virtual ). 没有一般禁止,但在构造函数中执行此操作时必须小心(因为您的基类是virtual )。 You must know for certain that the named base sub-object already exists. 您必须确定已命名的基础子对象已存在。 For non-virtual base classes, this is not an issue as they are constructed in order. 对于非虚拟基类,这不是问题,因为它们是按顺序构造的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM