Pub*_*bby 5 c++ methods virtual inheritance
如果我这样做:
Dog dog; //class with virtual methods
Cat cat; //class from same base as Dog
dog.eat(); //call virtual method
cat.eat(); //call virtual method
Run Code Online (Sandbox Code Playgroud)
然后eat()s将是正常的方法调用,不会要求v表 - 正确吗?我可以假设它会运行与非虚方法相同吗?
(是的,我知道编译器如何处理虚函数不在标准中 - 我想知道大多数编译器的作用)
当您使用object.member- 时,您不会取消引用指针,因此对方法没有影响virtual。Virtual仅当您有一个可以是多态的指针并且使用动态调度时才生效。
例如:
Cat cat;
Animal *cat2 = &cat;
cat.eat(); // direct call
//... a lot of other code and function calls that pass cat2 around, to avoid optimization
cat2->eat(); // dynamic dispatch
Run Code Online (Sandbox Code Playgroud)
根据评论编辑更正
| 归档时间: |
|
| 查看次数: |
199 次 |
| 最近记录: |