473,652 Members | 3,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(IB lahBlah, 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 2811
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(I BlahBlah, 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
5461
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
2221
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 if anyone can tell me why my forward class declarations not working. the member function findpaths can't seem to access the class Board when I try to pass it a board object by value. I put the Board class latrerbut also put in a forward...
6
5199
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 TESTDRIVER_HPP #include <ostream> namespace ns_testdriver{ using std::ostream;
11
2427
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
12157
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
3841
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? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
2
508
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
8612
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 myfile.h template<typename T,typename R> class some_class;
11
8310
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 device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
0
1467
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 file. I would like to apply the visitor pattern to some of these classes. One of the technicalities behind the visitor pattern is that one needs forward declarations of classes, since there are two class hierarchies which refer to each other.
0
8279
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8703
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.