forward declaration for a type definition

A

aaragon

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.

 
D

David Harmon

On Mon, 19 Nov 2007 18:43:58 -0800 (PST) in comp.lang.c++, aaragon
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.
 
K

Kai-Uwe Bux

aaragon said:
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
 
A

aaragon

You can forward declare classes.

You cannot, however, forward declare an identifier as a type name to be
defined (e.g., by typedef) later.


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, MyOwnClass2> ClassA;

// 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<SomeClass1> MyOwnClass1;
typedef MyOwnClass<SomeClass2> MyOwnClass2;

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

Victor Bazarov

aaragon said:
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, MyOwnClass2> ClassA;

// 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<SomeClass1> MyOwnClass1;
typedef MyOwnClass<SomeClass2> MyOwnClass2;

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
 
A

aaragon

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).










V

ClassA is defined by a library. It is actually a class template, so
the correct type has to be obtained through a type definition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top