虚函数

信息来源: 牛津词典, LDOCE, The Bing Bang Theory.

Our deputy manager is the virtual head of the business. 我们的副经理是公司的实际负责人。

class Homo_sapiens {
 virtual void talk() = 0;
};

class Person : Homo_sapiens {
 virtual void talk() { puts("我打酱油的。"); }
}

class Worker : Person {
 virtual void talk() {
   puts("Hei-you-hei-you, hei-you-hei-you");
   puts("Ai-da-da-ai-da, ai-da-da-ai-da");
   puts("Take the man down.")
 }
};

class DeputyManager : Worker {
 virtual void talk() {
   puts("I'm the virtual head. How magnificent am I.");
   puts("Hey! Less talk, more work!")
 }
};

Homo_sapiens* hs1 = new Worker;
Homo_sapiens* hs2 = new DeputyManager;
hs1->talk();
hs2->talk();

virtual

1. very nearly a particular thing
2. made, done, seen etc on the Internet or on a computer, rather than in the real world
a. being or acting as what is described, but not accepted as such in name or officially 事实上的, 实际上的, 实质上的

不过:  In short, a virtual function defines a target function to be executed, but the target might not be known at compile time. Virtual functions allow a program to call methods that don't necessarily even exist at the moment the code is compiled. "虚"也有"虚"的道理。叫"v函数":-)算了。

// In 1.cpp which was compiled long long ago, gcc -c 1.cpp
void fn(Person* p) { p->talk(); }

// In 2.cpp: gcc 2.cpp 1.o 1.cpp被编译时Worker::talk()尚不存在
fn(new Worker());

virtually:

1. almost, pratically
2. on a computer, rather than in the real world

Virtual reality (VR) is a simulated experience that can be similar to or completely different from the real world. Applications of virtual reality include entertainment (e.g. video games) and education (e.g. medical or military training). Other distinct types of VR-style technology include augmented reality and mixed reality, sometimes referred to as extended reality or XR.

posted @ 2021-11-26 09:47  Fun_with_Words  阅读(69)  评论(0编辑  收藏  举报









 张牌。