Home > Back-end >  Pure virtual function must be empty function?
Pure virtual function must be empty function?

Time:12-06

The answer to that "pure virtual function is a special kind of virtual functions, it is an empty function" this sentence is wrong,
My understanding is pure virtual function doesn't even have the body of the function, and an empty function also has the function body at least... So pure virtual function must be not empty function...
1. My understanding is correct
2. Could you please tell me can, for example, empty function? Empty common function, and empty function,,
Oh very stupid question...
Thank you bosses, the honour to sneak a look,

CodePudding user response:

To seek a wave again! Children find the following examples in baidu encyclopedia, want to know the following function types in chestnut is not void obviously why not return a value in the function body??????? Don't write the return can be written??????? But don't write returns a random value! There is an empty function in return can have? Good disorderly problem......
Example:
The class hierarchy
{
Public:
Virtual float GetArea ()=0;//pure virtual function
Virtual float GetPerim ()=0;//pure virtual function
};
The pure virtual function into the empty function
Is the following
The class hierarchy
{
Public:
Virtual float GetArea
{}//empty function
Virtual float GetPerim
{}//empty function
};

CodePudding user response:

Emm mainly is to ask hollow function can return

CodePudding user response:

As a novice, I talk about my understanding:

Empty function should be empty function, no statement, a statement, it is not empty function,

Function body is the definition of function, pure virtual function is not defined, only declare that there is no statement in empty function definition, I like your understanding,

Have a pure virtual function of class is an abstract class, cannot be instantiated, free class function but not pure virtual functions, can be instantiated,

Virtual functions can have return, also can not, no return and no other statements is the empty function,

Virtual float GetArea {}//empty function
No return, because of virtual functions, if his class is not need to use this function, you can don't have to write a statement first,
Virtual function reflects the c + + polymorphism,

CodePudding user response:

Pure virtual functions in addition to the mandatory overloaded with ordinary virtual function no difference, the same can have the body of the function...

CodePudding user response:

I think the building Lord consider this material?

CodePudding user response:

Empty function is nothing more than an ordinary function, the function inside without any code, I think it is the return statement can have meaning, such as return PI. This should not be empty function
Virtual functions is likely to be the top of the function call, so it is the function; And pure virtual functions is usually will not be used, so the c + + language implementation may simply don't have any entity,
A casual said of pure virtual functions is also can be implemented, then it is certainly not a empty function,
  • Related