| View previous topic :: View next topic |
| Author |
Message |
Jon Guest
|
Posted: Tue Aug 31, 2004 12:10 am Post subject: Defining classes in terms of each other |
|
|
How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jakacki Guest
|
Posted: Tue Aug 31, 2004 10:16 am Post subject: Re: Defining classes in terms of each other |
|
|
| Quote: | How can I do something like this:
|
class YZ; // <--- add this
| Quote: |
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
|
BR
Grzegorz
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joe Gottman Guest
|
Posted: Tue Aug 31, 2004 10:17 am Post subject: Re: Defining classes in terms of each other |
|
|
"Jon" <jnovak (AT) gmail (DOT) com> wrote
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
You just have to forward-declare class YZ.
class YZ;
class XY {
YZ *yz;
};
class YZ {
XY *xy;
};
Note that works with classes containing pointers (or references) to each
other. It would not work if you wanted both classes to contain actual
instances of each other.
Joe Gottman
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Guest
|
Posted: Tue Aug 31, 2004 12:13 pm Post subject: Re: Defining classes in terms of each other |
|
|
Hi Jon,
just put a so called foreward definition before class XY.
class YZ;
class XY
{
YZ* yz;
};
....
Cheers
Frank
Jon wrote:
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Tue Aug 31, 2004 12:15 pm Post subject: Re: Defining classes in terms of each other |
|
|
"Jon" <jnovak (AT) gmail (DOT) com> wrote
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
Of course it is. The thing which you need is called a forward declaration.
You may want to check the FAQ here -
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-38.11
-Sharad
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Fisher Guest
|
Posted: Tue Aug 31, 2004 12:19 pm Post subject: Re: Defining classes in terms of each other |
|
|
"Jon" <jnovak (AT) gmail (DOT) com> wrote
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
Use a forward declaration:
class YZ;
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
David Fisher
Sydney, Australia
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
void Guest
|
Posted: Tue Aug 31, 2004 7:27 pm Post subject: Re: Defining classes in terms of each other |
|
|
Jon wrote:
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
It is possible, use forward declaration:
class YZ;
Best
Darek Ostolski
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel Salters Guest
|
Posted: Tue Aug 31, 2004 7:28 pm Post subject: Re: Defining classes in terms of each other |
|
|
[email]jnovak (AT) gmail (DOT) com[/email] (Jon) wrote in message news:<c159ff46.0408300116.628d5f32 (AT) posting (DOT) google.com>...
| Quote: | How can I do something like this:
|
class YZ; // This is a forward declaration
| Quote: | class XY {
// Use the forward declaration
YZ* yz;
};
// The class YZ that was declared earlier
class YZ {
XY* xy;
};
|
Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Philipp Bachmann Guest
|
Posted: Tue Aug 31, 2004 7:30 pm Post subject: Re: Defining classes in terms of each other |
|
|
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
Sure it is. You need what's called a "forward declaration":
class YZ; // This is the "forward declaration".
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Given you have a forward declaration in place, you then can declare
pointers or references to instances of the forward declared class or
declare the class as a "friend" of another one.
You can't build instances of a forward declared class, though, because
it's type remains incomplete untils its very declaration.
Cheers,
Philipp.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Friedhelm Hoerner Guest
|
Posted: Tue Aug 31, 2004 7:30 pm Post subject: Re: Defining classes in terms of each other |
|
|
[email]jnovak (AT) gmail (DOT) com[/email] (Jon) wrote in message news:<c159ff46.0408300116.628d5f32 (AT) posting (DOT) google.com>...
| Quote: | How can I do something like this:
|
Insert the following line here:
class YZ; // forward declaration.
| Quote: | class XY {
YZ* yz; // a pointer to a class declared somewhere else
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
Of course it is, all you need is the forward declaration above.
It informs the compiler that there is a class "YZ", defined somewhere
else. You may use now anything that doesn't need to "know" about the
memory layout of the class such as pointers and references.
the following definition would therefore be an error:
class YZ; // forward declaration.
class XY {
YZ yz; // Error: the size of the object is unknown yet...
};
Friedhelm
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Motti Lanzkron Guest
|
Posted: Tue Aug 31, 2004 7:31 pm Post subject: Re: Defining classes in terms of each other |
|
|
Joe Gottman wrote:
| Quote: | You just have to forward-declare class YZ.
class YZ;
class XY {
YZ *yz;
};
class YZ {
XY *xy;
};
Note that works with classes containing pointers (or references) to each
other. It would not work if you wanted both classes to contain actual
instances of each other.
|
Yeah, for some reason C++ insists that classes be of finite size ;o)
The same is true for one class.
class AlefNull {
AlefNull imposible;
};
sizeof(AlefNull) == ?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pedro Ferreira Guest
|
Posted: Tue Aug 31, 2004 7:33 pm Post subject: Re: Defining classes in terms of each other |
|
|
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
Sure. Just forward declare YZ:
class YZ;
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
HTH,
Pedro
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jon hanson Guest
|
Posted: Tue Aug 31, 2004 7:49 pm Post subject: Re: Defining classes in terms of each other |
|
|
class YZ;
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
[email]jnovak (AT) gmail (DOT) com[/email] (Jon) wrote in message news:<c159ff46.0408300116.628d5f32 (AT) posting (DOT) google.com>...
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mike Bland Guest
|
Posted: Tue Aug 31, 2004 7:54 pm Post subject: Re: Defining classes in terms of each other |
|
|
[email]jnovak (AT) gmail (DOT) com[/email] (Jon) wrote in message news:<c159ff46.0408300116.628d5f32 (AT) posting (DOT) google.com>...
| Quote: | How can I do something like this:
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Is this not possible in C++?
|
Sure, just forward declare class YZ at the very beginning:
class YZ;
class XY {
YZ* yz;
};
class YZ {
XY* xy;
};
Mike
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|