What is the difference between an abstract functio

2019-01-03 00:48发布

What is the difference between an abstract function and a virtual function? In which cases is it recommended to use virtual or abstract? Which one is the best approach?

24条回答
Viruses.
2楼-- · 2019-01-03 01:03

Abstract method doesnt have an implementation.It is declared in the parent class. The child class is resposible for implementing that method.

Virtual method should have an implementation in the parent class and it facilitates the child class to make the choice whether to use that implementation of the parent class or to have a new implementation for itself for that method in child class.

查看更多
做个烂人
3楼-- · 2019-01-03 01:04

The answer has been provided a number of times but the the question about when to use each is a design-time decision. I would see it as good practice to try to bundle common method definitions into distinct interfaces and pull them into classes at appropriate abstraction levels. Dumping a common set of abstract and virtual method definitions into a class renders the class unistantiable when it may be best to define a non-abstract class that implements a set of concise interfaces. As always, it depends on what best suits your applications specific needs.

查看更多
狗以群分
4楼-- · 2019-01-03 01:05

Most of the above examples use code - and they are very very good. I need not add to what they say, but the following is a simple explanation that makes use of analogies rather than code/technical terms.

Simple Explanation - Explanation using analogies

Abstract Method

Think George W Bush. He says to his soldiers: "Go fight in Iraq". And that's it. All he has specified is that fighting must be done. He does not specify how exactly that will happen. But I mean, you can't just go out and "fight": what does that mean exactly? do I fight with a B-52 or my derringer? Those specific details are left to someone else. This is an abstract method.

Virtual Method

David Petraeus is high up in the army. He has defined what fight means:

  1. Find the enemy
  2. Neutralise him.
  3. Have a beer afterwards

The problem is that it is a very general method. It's a good method that works, but sometimes is not specific enough. Good thing for Petraeus is that his orders have leeway and scope - he has allowed others to change his definition of "fight", according to their particular requirements.

Private Job Bloggs reads Petraeus' order and is given permission to implement his own version of fight, according to his particular requirements:

  1. Find enemy.
  2. Shoot him in the head.
  3. Go Home
  4. Have beer.

Nouri al Maliki also receives the same orders from Petraeus. He is to fight also. But he is a politician, not an infantry man. Obviously he cannot go around shooting his politican enemies in the head. Because Petraeus has given him a virtual method, then Maliki can implement his own version of the fight method, according to his particular circumstances:

  1. Find enemy.
  2. Have him arrested with some BS trumped up charges.
  3. Go Home
  4. Have beer.

IN other words, a virtual method provides boilerplate instructions - but these are general instructions, which can be made more specific by people down the army heirarchy, according to their particular circumstances.

The difference between the two

  • George Bush does not prove any implementation details. This must be provided by someone else. This is an abstract method.

  • Petraeus on the other hand does provide implementation details but he has given permission for his subordinates to override his orders with their own version, if they can come up with something better.

hope that helps.

查看更多
老娘就宠你
5楼-- · 2019-01-03 01:06

Binding is the process of mapping a name to a unit of code.

Late binding means that we use the name, but defer the mapping. In other words, we create/mention the name first, and let some subsequent process handle the mapping of code to that name.

Now consider:

  • Compared to humans, machines are really good at searching and sorting
  • Compared to machines, humans are really good at invention and innovation

So, the short answer is: virtual is a late binding instruction for the machine (runtime) whereas abstract is the late binding instruction for the human (programmer)

In other words, virtual means:

“Dear runtime, bind the appropriate code to this name by doing what you do best: searching

Whereas abstract means:

“Dear programmer, please bind the appropriate code to this name by doing what you do best: inventing

For the sake of completeness, overloading means:

“Dear compiler, bind the appropriate code to this name by doing what you do best: sorting”.

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-03 01:06

From a C++ background, C# virtual corresponds to C++ virtual, while C# abstract methods corresponds to C++ pure virtual function

查看更多
Melony?
7楼-- · 2019-01-03 01:07

From general object oriented view:

Regarding abstract method: When you put an abstract method in the parent class actually your are saying to the child classes: Hey note that you have a method signature like this. And if you wanna to use it you should implement your own!

Regarding virtual function: When you put a virtual method in the parent class you are saying to the derived classes : Hey there is a functionality here that do something for you. If this is useful for you just use it. If not, override this and implement your code, even you can use my implementation in your code !

this is some philosophy about different between this two concept in General OO

查看更多
登录 后发表回答