473,408 Members | 1,738 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,408 software developers and data experts.

Forward declarations

Hi All,

In some headers meant to work with both C and C++ the following is often
found:

typedef struct tagMyStruct { /*some members*/ } MyStruct;

Can I forward-declare MyStruct somehow?

Or more generally, can I forward declare a typedef or an enum? Also, is
there any reason why there should be difference between forward declarations
of classes and structs (i.e. why I need to explicitly write keyword 'struct'
or 'class' when forward declaring). In some cases I don't immediately know
what to write, because, for example, the type definition in original header
file is generated with some macro, such as this:

STDINTERFACE(IBlahBlah, IBaseBlahBlah) {
/* Members */
}

Now I need to search for the definition of STDINTERFACE to see how it works.
Even if I find it, it still may silently change in some future version of
the header file, and cause my compilation to fail. Wouldn't allowing a
generic form of forward declaration, without introducing a new keyword:

typename IBlahBlah;

be beneficial to C++?

best regards,
Marcin


Jul 22 '05 #1
2 2752
On Tue, 11 May 2004 16:35:12 +0200, "Marcin Kalicinski"
<ka****@poczta.onet.pl> wrote:
Hi All,

In some headers meant to work with both C and C++ the following is often
found:

typedef struct tagMyStruct { /*some members*/ } MyStruct;

Can I forward-declare MyStruct somehow?
Sure, you can say:

typedef struct tagMyStruct MyStruct;

and complete the definition later on, if necessary.

Or more generally, can I forward declare a typedef or an enum?
typedefs, I don't think so. Under Comeau, in C mode, if I did something
like this:
typedef b;
it took it to mean as if I'd said:
typedef int b;
(and emitted a suitable warning).

With enums, Comeau tells me that forward-declaring them is nonstandard. So
it may be supported, but isn't portable.
Also, is
there any reason why there should be difference between forward declarations
of classes and structs (i.e. why I need to explicitly write keyword 'struct'
or 'class' when forward declaring).
Well, you began this post by saying "In some headers meant to work with
both C and C++", so there's an obvious reason to prefer struct right there.
In some cases I don't immediately know
what to write, because, for example, the type definition in original header
file is generated with some macro, such as this:

STDINTERFACE(IBlahBlah, IBaseBlahBlah) {
/* Members */
}

Now I need to search for the definition of STDINTERFACE to see how it works.
Even if I find it, it still may silently change in some future version of
the header file, and cause my compilation to fail. Wouldn't allowing a
generic form of forward declaration, without introducing a new keyword:

typename IBlahBlah;

be beneficial to C++?
I would hope that any such header file gives you a documented way to /use/
the facilities it generates, so that such use is immune to future
implementation changes. If you think you're using something that's going to
morph out from under you in the next release, you'll have to get creative
with, perhaps, some preprocessor (or typedef) hackery of your own. Don't
hold your breath waiting for changes in the language to support you here.
-leor

best regards,
Marcin


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Marcin Kalicinski wrote:
Hi All,

In some headers meant to work with both C and C++ the following is often
found:

typedef struct tagMyStruct { /*some members*/ } MyStruct;

Can I forward-declare MyStruct somehow?
Yes:
struct tagMyStruct;
should work.

Or more generally, can I forward declare a typedef or an enum? Also, is

The lack of a forward declaration of enum is my pet peeve also.
VC6.0,7.0 & 7.1 all support it as a language extension, and it was only
when I began writing cross platform code that I discovered that it was
not legal in c/c++. (It can help immensely in avoiding header spaghetti)
I realize there are issues regarding knowing the underlying type of
an enum, but if these compilers can handle it, then it is clearly
possible. Nonetheless, I am not holding my breath.

[Snip] best regards,
Marcin


Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
10
by: Alan Lee | last post by:
Hi i am writing a small simulation for a bunch of atoms jumping around on a surface. I know i am a crappy programmer since i am not very familiar with object oriented languages. I am woindering...
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
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
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? ...
2
by: Carlos Martinez Garcia | last post by:
Hi all: I usually make forward declarations in headers. Something like this: class MyClass; Now, I need a reference to a type defined like this (traditional C Style): typedef struct {
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
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...
0
by: Rune Allnor | last post by:
Hi all. I have these classes, implemented as templates in header files. Right now I have one file per class, where both the declarations and implementations of one class are located in each...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.