By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

forward declaration for a type definition

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
On Mon, 19 Nov 2007 18:43:58 -0800 (PST) in comp.lang.c++, aaragon
<al**************@gmail.comwrote,
>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

P: n/a
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

P: n/a
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<MyOwnClass1, MyOwnClass2ClassA;

// 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<SomeClass1MyOwnClass1;
typedef MyOwnClass<SomeClass2MyOwnClass2;

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

P: n/a
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<MyOwnClass1, MyOwnClass2ClassA;

// 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<SomeClass1MyOwnClass1;
typedef MyOwnClass<SomeClass2MyOwnClass2;

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

P: n/a
On Nov 20, 11:11 am, "Victor Bazarov" <v.Abaza...@comAcast.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<MyOwnClass1, MyOwnClass2ClassA;
// 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<SomeClass1MyOwnClass1;
typedef MyOwnClass<SomeClass2MyOwnClass2;
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 discussion thread is closed

Replies have been disabled for this discussion.