如果不是纯的,使用虚拟成员函数? [英] A virtual member function is used if it is not pure?
问题描述
C ++ 03 3.2.2
...如果对象或非重载函数的名称出现在潜在求值表达。如果它不是纯的,则使用虚拟成员函数...
C++03 3.2.2
...An object or non-overloaded function is used if its name appears in a potentially-evaluated expression. A virtual member function is used if it is not pure...
然后在
我们有: 每个程序都应该包含在该程序中使用的每个非内联函数或对象的一个定义;无需诊断。定义可以在程序中显式显示,它可以在标准或用户定义的库中找到,或者(在适当的时候)被隐式定义(见12.1,12.4和12.8)。
在使用它的每个翻译单元中都必须定义一个内联函数。
不使用纯虚拟函数。 ODR仅适用于所使用的函数。这不意味着以下是合法的?我猜的答案是不,它不,但后来我不明白为什么。
Along the lines I am reading: a pure virtual function is not used. The ODR applies only to functions which are used. Doesn't this imply that the following would be legal? I am guessing the answer is no, it doesn't, but then I can't understand why.
//x.h
struct A
{
virtual void f() = 0;
};
//y.cpp
#include "x.h"
void A::f()
{
}
//z.cpp
#include "x.h"
#include <iostream>
void A::f()
{
std::cout << "Hello" << std::endl;
}
//main.cpp
#include "x.h"
struct B:A
{
virtual void f()
{
A::f();
}
};
int main()
{
A* p = new B;
p->f();
}
推荐答案
独家。
如果它不是纯的,使用虚函数,并不意味着相反。如果一个虚函数是纯的,并不意味着它必然不被使用。它仍然可以被使用如果其名称出现在潜在求值的表达式中,例如在您的示例中: A :: f();
。
这篇关于如果不是纯的,使用虚拟成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!