473,807 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

forward declaration for a type definition

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 to
do it.
Thank you.


Nov 20 '07 #1
5 7012
On Mon, 19 Nov 2007 18:43:58 -0800 (PST) in comp.lang.c++, aaragon
<al************ **@gmail.comwro te,
>Hello, I was wondering if it is possible to forward declare a type
definition. If so, what is the way to do it?
Sure, for example,

class foobar;

Of course the type is "incomplete " without the full definition and there
are limits to what you can do with it; you can create pointers to it but
not instances of it.

Nov 20 '07 #2
aaragon wrote:
Hello, I was wondering if it is possible to forward declare a type
definition.
You can forward declare classes.

You cannot, however, forward declare an identifier as a type name to be
defined (e.g., by typedef) later.
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 to
do it.
Why?

If you are in a situation where you write code for a type (not necessarily
just a class) yet unknown, consider writing a template.
Best

Kai-Uwe Bux
Nov 20 '07 #3
On Nov 19, 10:16 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
aaragon wrote:
Hello, I was wondering if it is possible to forward declare a type
definition.

You can forward declare classes.

You cannot, however, forward declare an identifier as a type name to be
defined (e.g., by typedef) later.
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 to
do it.

Why?

If you are in a situation where you write code for a type (not necessarily
just a class) yet unknown, consider writing a template.

Best

Kai-Uwe Bux
I'm using code from a library so I need to create type definitions for
specific types. However, the type definition is defined by using a
templated class that uses it at the same time. Something like this:

typedef Class<MyOwnClas s1, MyOwnClass2Clas sA;

// Then, in the definition of MyOwnClass
template <class SomeClass>
class MyOwnClass {

// uses type definition ClassA
someFun(ClassA& );

};

// and then the actual instantiations of MyOwnClass
typedef MyOwnClass<Some Class1MyOwnClas s1;
typedef MyOwnClass<Some Class2MyOwnClas s2;

so you can see, that the class MyOwnClass depends on types that are
not yet instantiated.

The way I solved this is by moving the typedefinition of MyOwnClass1
and MyOwnClass2 before the first typedef and using a forward
declaration of the template class MyOwnClass. Very messy eh???? It
turns out that this cyclic dependence is because of the use of a
Visitor class that I need to have for MyOwnClass (visitor design
pattern).


Nov 20 '07 #4
aaragon wrote:
On Nov 19, 10:16 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>aaragon wrote:
>>Hello, I was wondering if it is possible to forward declare a type
definition.

You can forward declare classes.

You cannot, however, forward declare an identifier as a type name to
be defined (e.g., by typedef) later.
>>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 to
do it.

Why?

If you are in a situation where you write code for a type (not
necessarily just a class) yet unknown, consider writing a template.

Best

Kai-Uwe Bux

I'm using code from a library so I need to create type definitions for
specific types. However, the type definition is defined by using a
templated class that uses it at the same time. Something like this:

typedef Class<MyOwnClas s1, MyOwnClass2Clas sA;

// Then, in the definition of MyOwnClass
template <class SomeClass>
class MyOwnClass {

// uses type definition ClassA
someFun(ClassA& );
What's wrong with just declaring 'ClassA' a class instead of making
it a typedef? To declare a function that accepts a reference to it
the compiler only needs to know that it's a class (and not an array
or a function, for example).
>
};

// and then the actual instantiations of MyOwnClass
typedef MyOwnClass<Some Class1MyOwnClas s1;
typedef MyOwnClass<Some Class2MyOwnClas s2;

so you can see, that the class MyOwnClass depends on types that are
not yet instantiated.

The way I solved this is by moving the typedefinition of MyOwnClass1
and MyOwnClass2 before the first typedef and using a forward
declaration of the template class MyOwnClass. Very messy eh???? It
turns out that this cyclic dependence is because of the use of a
Visitor class that I need to have for MyOwnClass (visitor design
pattern).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 20 '07 #5
On Nov 20, 11:11 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
aaragon wrote:
On Nov 19, 10:16 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
aaragon wrote:
Hello, I was wondering if it is possible to forward declare a type
definition.
You can forward declare classes.
You cannot, however, forward declare an identifier as a type name to
be defined (e.g., by typedef) later.
>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 to
do it.
Why?
If you are in a situation where you write code for a type (not
necessarily just a class) yet unknown, consider writing a template.
Best
Kai-Uwe Bux
I'm using code from a library so I need to create type definitions for
specific types. However, the type definition is defined by using a
templated class that uses it at the same time. Something like this:
typedef Class<MyOwnClas s1, MyOwnClass2Clas sA;
// Then, in the definition of MyOwnClass
template <class SomeClass>
class MyOwnClass {
// uses type definition ClassA
someFun(ClassA& );

What's wrong with just declaring 'ClassA' a class instead of making
it a typedef? To declare a function that accepts a reference to it
the compiler only needs to know that it's a class (and not an array
or a function, for example).


};
// and then the actual instantiations of MyOwnClass
typedef MyOwnClass<Some Class1MyOwnClas s1;
typedef MyOwnClass<Some Class2MyOwnClas s2;
so you can see, that the class MyOwnClass depends on types that are
not yet instantiated.
The way I solved this is by moving the typedefinition of MyOwnClass1
and MyOwnClass2 before the first typedef and using a forward
declaration of the template class MyOwnClass. Very messy eh???? It
turns out that this cyclic dependence is because of the use of a
Visitor class that I need to have for MyOwnClass (visitor design
pattern).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
ClassA is defined by a library. It is actually a class template, so
the correct type has to be obtained through a type definition.
Nov 20 '07 #6

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

Similar topics

11
37104
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't explicitly say about enum's forward declaration, but one example shows it in 18.2.1, clause 4:
5
1884
by: John Gabriele | last post by:
I'm hoping someone can please help me remember the C++ rule: When you're writing a header file for a class (say, some_namespace::Bar), and that class makes use of another class (some_namespace::Foo), -------------------------------- snip -------------------------------- #ifndef GUARD_Foo_HPP #define GUARD_Foo_HPP namespace some_namespace {
2
2298
by: John Ratliff | last post by:
I'm having issues with forward declarations and possibly member variables. Can you declare a member variable and pass it parameters. class x { private: y obj(this); } Is that valid? I'm getting a lot of problems, but it may not be due to
3
20615
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward declaration? class CEnemy : public CEntity { public:
11
2815
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
23
3872
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)
4
5376
by: Steve | last post by:
Hi, I always though to return an instance of a class by value, it had to be defined - i.e. forward declaration isn't good enough? Consider the following code snippet: class RGBA; class Colour
7
5088
by: RedLars | last post by:
Need some help with a couple of questions. Previously I have seen this declaration; struct Student { int age; char name; }; and this definition of an object; struct Student stud;
11
8340
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
9599
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
10626
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
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
10374
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
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
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
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.