我知道虚函数本质上是包含在vtable上的函数指针,这使得多态调用由于间接等而变慢.但是当调用是确定性的时候我想知道编译器优化.确定性,我指的是以下情况:
struct Foo
{
virtual void DoSomething(){....}
};
int main()
{
Foo myfoo;
myfoo.DoSemthing();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
struct Foo
{
virtual void DoSomething();
};
struct Bar : public Foo
{
virtual void DoSomething();
};
int main()
{
Foo* a = new Foo();
a->DoSomething(); //Overhead ? a doesn't seem to be able to change nature.
Foo* b = new Bar();
b->DoSomething(); //Overhead ? It's a polymorphic call, but b's nature is deterministic.
Bar* c = new Bar();
c->DoSomething(); //Overhead …
Run Code Online (Sandbox Code Playgroud)