Any way to forward declare a function prototype?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jsyjr
    New Member
    • Apr 2010
    • 3

    Any way to forward declare a function prototype?

    I make regular use of forward class declarations and pointers to such classes.

    I now have a need to pass a function pointer through a number of layers. I would prefer to include the header that declares my function pointer's prototype only into the module that dereferences a function pointer rather than into each layer that simply passes along that pointer value.

    Is this possible?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A function declaration is a forward declaration in so far as it does not define a function only declares it as existing in the same way a class forward declaration only declares the existence of a class and does not define it.

    Comment

    • jsyjr
      New Member
      • Apr 2010
      • 3

      #3
      You misunderstood my question. I am not asking about declaring a function. I am looking for the analog of a forward class declaration. I can write:

      class foo;

      void bar(foo*);

      void waz(foo* p)
      {
      bar(p);
      }

      Notice that waz knows nothing about class foo other than its name. Perhaps bar will have access to foo's complete description. Perhaps bar will simply pass p further along. Who cares? Only those sites that dereference a foo* pointer. All others can write "class foo;" and be done.

      Similarly I know that I can write:

      typedef void foo(int, double);

      void bar(foo*);

      void waz(foo* p)
      {
      bar(p);
      }

      The difference is that in this case the identifier foo not only is known to denote a function type but further includes the full prototype. This forces me into one of two unpleasant scenarios:

      1) clone the typedef at multiple sites (yuck! fragile!)
      2) stick the typedef in a header and include it everywhere the function pointer type is mentioned.

      Notice the asymetry: in the case of a data object I only needed to provide a complete description of class foo at those points where I want to dereference a foo*.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Notice the asymetry: in the case of a data object I only needed to provide a complete description of class foo at those points where I want to dereference a foo*.
        No I notice it being exactly the same, in both the case of the function and the class you only have to provide a declaration of it in order to use a pointer to it. In neither case do you have to provide the definition in order to use a pointer.

        Yes the declaration of a function is more complex than the declaration of a class.

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Go with option 2, the header, for consistency and maintenance reasons

          Comment

          • jsyjr
            New Member
            • Apr 2010
            • 3

            #6
            Banfa: I think the answer is that, unlike classes, functions allow overloading so a naked symbol is insufficient.

            jkmyoung: I agree. I never seriously considered choosing option 1.

            Comment

            Working...