Home > Net >  Is a parameter name neccesary for virtual method definition in C ?
Is a parameter name neccesary for virtual method definition in C ?

Time:11-03

Here is the code:

virtual bool myFunction(const Waypoints& /*waypoints*/) {
    return false;
}

For my understanding, virtual function is for late / dynamic binding. bool is the return type. const Waypoint& is a constant reference. When it is used to formal parameters, it avoids value copy and forbids being changed by the function.

Now, my question is, shall we need a variable name for the formal parameter of this function somehow? I mean, /*waypoints*/ are simply comments, right? Where are the formal parameters then?

CodePudding user response:

The method has one formal parameter of type const Waypoints&. It is unnamed, because it is not used in the method body. This might make sense, because other implementations of the same method might use it (note that the method is virtual). Whether the name of the parameter /*waypoints*/ is commented out, left there or removed altogether is a matter of taste. Some compilers issue a warning when a formal parameter (that does have a name) is not used in the method body, so this might be the reason it was commented out.

CodePudding user response:

A declaration of a function (member or free) never needs names for parameters (although they can be useful for the reader). A definition of a function only needs names for parameters that are used in the body of the function. You can have different names in different declarations, they are ignored.

Where are the formal parameters then?

There is an parameter of type const Waypoints&. It is unnamed in the definition, which is fine because it is unused.

  •  Tags:  
  • c
  • Related