title: UML representation for C/C++ function pointers
body: <p>What would be the best representation of a C/C++ function pointer (fp) in an UML structural diagram?</p>
  
  <p>I'm thinking about using an interface element, may be even if 'degenerate' with the constraint of having at most a single operation declared.</p>
  
  <p>I found some proposal in this document: <a href="http://www.google.de/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=4&amp;cad=rja&amp;ved=0CEoQFjAD&amp;url=http://gforge.enseeiht.fr/docman/view.php/122/4145/TPC_UML_C_UserGuide.pdf&amp;ei=YVy6UKT7HorOswa_uoGgCQ&amp;usg=AFQjCNHp2F2Kx3A_JaxuTsKPorAnksqyoQ&amp;sig2=s66MiFyjtRhDSqGJpa-oQQ" rel="noreferrer">C and UML Synchronization User Guide, Section 5.7.4</a>. But this sounds quite cumbersome and not very useful in practice. Even if right from a very low level of semantic view. Here's a diagram showing their concept briefly:
  <img src="https://i.stack.imgur.com/JrnzP.png" alt="enter image description here"></p>
  
  <p>IMHO in C and C++ function pointers are used as such a narrowed view of an interface which only provides a single function and it's signature. In C fp's would be used also to implement more complex interfaces declaring a struct containing a set of function pointers.</p>
  
  <p>I think I can even manage to get my particular UML tool (Enterprise Architect) to forward generate the correct code, and synchronizing with code changes without harm. </p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Would declaration of fp's as part of interface elements in UML proivde a correct semantic view?</li>
  <li>What kind of stereotype should be used for single fp declaration? At least I need to provide a typedef in code <strike>so this would be my guts choice.</strike>(I found this stereotype is proprietary for Enterprise Architect) and I need to define an appropriate stereotype to get the code generation adapted. Actually I have chosen the stereotype name 'delegate', does this have any implications or semantic collisions?</li>
  <li>As for C++, would be nesting a 'delegate' sterotyped interface with in a class element enough to express a class member function pointer correctly?</li>
  </ol>
  
  <p>Here's a sample diagram of my thoughts for C language representation:
  <img src="https://i.stack.imgur.com/iMYGe.png" alt="C language interface and function pointer implementation"></p>
  
  <p>This is the C code that should be generated from the above model:</p>
  
  <pre><code>struct Interface1;
  
  typedef int (*CallbackFunc)(struct Interface1*);
  
  typedef struct Interface1
  {
      typedef void (*func1Ptr)(struct Interface1*, int, char*);
      typedef int (*func2Ptr)(struct Interface1*, char*);
      typedef int (*func3Ptr)(struct Interface1*, CallbackFunc);
  
      func1Ptr func1;
      func2Ptr func2;
      func3Ptr func3;
  
      void* instance;
  };
  
  /* The following extern declarations are only dummies to satisfy code
   * reverse engineering, and never should be called.
   */
  extern void func1(struct Interface1* self, int p1, char* p2) = 0;
  extern int func2(struct Interface1* self, char*) = 0;
  extern int func3(struct Interface1* self, CallbackFunc p1) = 0;
  </code></pre>
  
  <p><strong>EDIT:</strong><br>
  The whole problem boils down what would be the best way with the UML tool at hand and its specific code engineering capabilities. Thus I have added the <a href="/questions/tagged/enterprise-architect" class="post-tag" title="show questions tagged &#39;enterprise-architect&#39;" rel="tag">enterprise-architect</a> tag.</p>