C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Defining classes in terms of each other

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jon
Guest





PostPosted: Tue Aug 31, 2004 12:10 am    Post subject: Defining classes in terms of each other Reply with 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
jakacki
Guest





PostPosted: Tue Aug 31, 2004 10:16 am    Post subject: Re: Defining classes in terms of each other Reply with quote



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





PostPosted: Tue Aug 31, 2004 10:17 am    Post subject: Re: Defining classes in terms of each other Reply with quote




"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





PostPosted: Tue Aug 31, 2004 12:13 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

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





PostPosted: Tue Aug 31, 2004 12:15 pm    Post subject: Re: Defining classes in terms of each other Reply with quote


"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





PostPosted: Tue Aug 31, 2004 12:19 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

"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





PostPosted: Tue Aug 31, 2004 7:27 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

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





PostPosted: Tue Aug 31, 2004 7:28 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

[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





PostPosted: Tue Aug 31, 2004 7:30 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

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





PostPosted: Tue Aug 31, 2004 7:30 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

[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





PostPosted: Tue Aug 31, 2004 7:31 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

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





PostPosted: Tue Aug 31, 2004 7:33 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

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





PostPosted: Tue Aug 31, 2004 7:49 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

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





PostPosted: Tue Aug 31, 2004 7:54 pm    Post subject: Re: Defining classes in terms of each other Reply with quote

[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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.