季良 发表于 2013-7-30 13:48:36

[google面试题]What 
is 
a
 virtual 
function/method?

 (in
C++)

宋倩倩 发表于 2013-7-30 13:56:52

A method is being "virtual" simply describes its behavior whenworking with superclasses and subclasses.Assume class B is a subclass of class A.Also assume both classes A and B have a method "bar()". Let's say we have thefollowing code inC ++:A *foo = new B();foo->bar();If the method "bar()" is declared to be virtual,then when we call foo‐>bar(),themethod found in class B will be run.This is how Java always handles methods andit's usually what we want to happen.However,if the method bar() is not declared to be virtual,then this code will run the method found in class A when we callfoo‐>bar().
页: [1]
查看完整版本: [google面试题]What 
is 
a
 virtual 
function/method?

 (in
C++)