开发者

Could a set of function differing in the name depending on the operand types called theoritically statically polymorphic in C language?

Does a set of functions doing exactly one word but differing in the name like atoi , atol, atoll etc called theoretically polymorphic?

For example I have a function say swap which needs to operate on different kind of data types. So i have one function/operation for which i have different implementations, but unfortunately because the language does not support using the same function name, to get such functionality i need to make variants swap_i , swap_l, swap_f, swap_str etc etc, and have to call them manually depending on the operands.

When designing the code, one would have designed this as one function, with different implementations, which are invoked depending upon the operands. But in this case the programmer instead of the compiler needs to make the static binding of the functions. If the programmer implemented it in C++ then the design would be the same (as he/she has followed an OOD approach), but in this case the static binding could be done by the compiler without bothering the user.

Now, if in the design approach swap was a polymorphic function, will such a difference in C and C++ design implementation will make the difference, so that we cannot call such a C implementation not polymorphic and the C++ implementation polymorphic ?

EDIT1:

Another example:

say we send a union with different possible data types packed in and a structure wrapping it with a variable indicating which component of the union to be used. Then we can use only one function name swap without variants. After we receive the structure in the function, we can internally do operations depending on the datatype active in the union

  struct _generic {
     int type;
     union {
              int a;
              float b;
              char c;
     } component;
  } variable;

  swap (struct _generic var2, struct _generic var1)
  {
     if (var1.type == INTEGER)
     {
         /* code for integer */
     }
     else if (var1.type == FLOAT)
     {
        /* c开发者_JS百科ode for float */
     }
      .
      .
      .
  }

In this case we use another way.

Also note the ellipses operator ... like in printf with this also we can get some builtin feature.

The main point is that, C language does not provide direct implementation of OOD s , but can we use the OOD terminology on the implementation on the C implementation, if it is designed by following an object oriented design?


Suppose the difference were two characters, or three, or ten? Would the different functions be polymorphic? I would say, no, they would not be. So what is special about 1 character?


It's not polymorphic, because the swap function variants cannot have more than one signature. The entire purpose of polymorphism is to limit the maintenance done by the programmer, and thinking of it as just overloading but done by the human is definitely the wrong way of thinking about it. The trick is in the term- "poly" and "morphism". There's nothing multiple about any of these functions- they just happen to provide similar functionality under similar names, and there's no morphing at all between them, so they are most assuredly not polymorphic.


I think that you are mixing different concepts and languages, and that is a recipe for confusion. What you are calling polymorphic is usually called overloaded in C++: there are different functions that share a name. Polymorphism is usually referred to the property of overriding the behavior of a function in a base class. The function itself will be one with a fixed set of name, arguments and return type (depending on the language there is some space for covariant return types or contravariant arguments).

As to whether it is appropriate to call a manual implementation in C as overloads, I don't think it is. If you extend and abuse the term, you could make it fit, in exactly the same way that you can program object oriented assembler (some assemblers allow you to define structures, you can add addresses to other code symbols as attributes to the structures... does it make that an object oriented assembler?)


C does not exhibit polymorphism. http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 for information on what polymorphism is. For the C functions to be considered even remotely polymorphic the functions would all have to have the same name, but operate on different data types (aka function overloading).


In my opinion it is not incorrect, because the polymorphism and other object oriented terminologies are not something which is language dependent, and is a concept. Now, different languages provide different features with which the implementation of an object model is much easier, and some features are only possible which were not possible in other languages.

If one problem was modelled with OOD approach, and the same model is followed to make an implementation with C and C++ keeping the approach identical, where the difference only comes from the limited feature set of the language, and then they are compiled then both generate assembler code. The C++ outputs for a polymorphic function are not polymorphic anymore after it is converted to the asm.

The difference is that C does not provide features to implement OOD as easier as it can be done in C++ or other OOP languages.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜