473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Any way to forward declare a function prototype?

3
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?
Apr 4 '10 #1
5 7424
Banfa
9,065 Expert Mod 8TB
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.
Apr 4 '10 #2
jsyjr
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*.
Apr 4 '10 #3
Banfa
9,065 Expert Mod 8TB
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.
Apr 4 '10 #4
jkmyoung
2,057 Expert 2GB
Go with option 2, the header, for consistency and maintenance reasons
Apr 6 '10 #5
jsyjr
3
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.
Apr 6 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: qazmlp | last post by:
Is it a good style to re-forward declare the types, which were already forward declared in base header file, in derived class header file? // base.h class addrClass; class base { public:...
11
by: aleko | last post by:
This applies equally to function prototypes. Why do we have to tell the compiler twice? Other modern compiled languages don't have this requirement. Just curious, Aleko
2
by: Sergey Ilinsky | last post by:
Well, I've been working with JS for three years and have a great experience here. But! I still have no really acceptable answer to the following question: What is the principle difference between...
12
by: fox | last post by:
How do I (portably) make a forward reference to a static (I mean file-scope) variable? I've been using "extern" for years, for example: extern int x; int foo(void) { return x++; }
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
11
by: Martin Eisenberg | last post by:
Hi Antoine, just redirecting you... Antoine Trux wrote: > Hi, > > Is the following code legal: > > ------> code starts here <------ > #include <stddef.h>
3
by: Till Crueger | last post by:
Hi, I am trying to implement a tree in C and I have the folowing code: struct inner { struct node *left; struct node *right; }; struct leaf {
5
by: aaragon | last post by:
Hello, I was wondering if it is possible to forward declare a type definition. If so, what is the way to do it? I found a couple of messages where they say it's not possible but there must be a way...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.