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

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 6956
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
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<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
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...
5
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...
2
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...
3
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...
11
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
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? ...
4
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...
7
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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
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...

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.