我想知道如何解释这一机制:
class Base{
class Other;
virtual void test() = 0;
};
class Other: Base{
virtual void test() override;
};
void Other::test(){ /*do something*/}
看起来我有一个名为 Base 的基类。它包含一个继承自 Base 的嵌套类。所以如果我打电话:
Base obj;
obj.test(); <-- it trigers the test() function from the Other class, doesn't it?
给定的例子和下面的例子有什么区别:
class Base{
virtual void test() = 0;
};
class Other: Base{
virtual void test() override;
};
void Other::test(){ /*do something*/}
将 Other 类隐藏在 Base 类中有什么好处?
最佳答案
class Base{ virtual void test() = 0; }; Base obj; // #0
#0
格式错误,因为 Base
是 abstract class ,因为它至少有一个纯抽象成员函数。
Abstract class
Defines an abstract type which cannot be instantiated, but can be used as a base class.
A pure virtual function is a virtual function whose declarator has the following syntax:
declarator virt-specifier(optional) = 0
[...] An abstract class is a class that either defines or inherits at least one function for which the final overrider is pure virtual.
当您从基指针或引用(对于特定运行时调用)引用派生对象分派(dispatch)到虚函数时,会发生对多态对象的动态分派(dispatch)。
在下面的例子中:
struct Base {
virtual void test() const = 0;
};
struct A final : public Base {
void test() const override {}; // #1
};
struct B final : public Base {
void test() const override {}; // #2
};
void f(Base const& b) {
b.test(); // #3
}
在 #3
处对虚拟成员函数 test()
的调用可以分派(dispatch)给 A::test()
或 B::test()
,取决于函数 f
的参数。
f(A{}); // 'test()' call in 'f' dispatches to #1
f(B{}); // 'test()' call in 'f' dispatches to #2
what would be the benefit of hidding the Other class in the Base class?
在您的原始示例中,Base
类向自身声明了一个嵌套类(但未定义它),这意味着 Other
类在 Base
中声明的与派生自它的 Other
类不同。
https://stackoverflow.com/questions/66025120/