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 

Reference to a forward declared class as class member?

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





PostPosted: Fri Nov 19, 2004 3:07 pm    Post subject: Reference to a forward declared class as class member? Reply with quote



Dear all

is the definition of Foo::bar_, which is a reference to
a forward declared class, well defined according to the standard?

My gut feel tells me that it's not and that it works in g++ by
accident only.

foo.h
=====

class Bar; // in bar.h

class Foo {
public:
Foo(Bar& bar);
private:
Foo& operator=(const Foo&); // not implemented
private:
Bar& bar_;
};

foo.cc
======

#include "bar.h"

Foo::Foo(Bar& bar) : bar_(bar)
{
// empty
}

Stephan Brönnimann
[email]broeni (AT) osb-systems (DOT) com[/email]
Open source rating and billing engine for communication networks.

P.S. I know I can use a pointer to Bar instead.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Sat Nov 20, 2004 10:36 am    Post subject: Re: Reference to a forward declared class as class member? Reply with quote



Stephan Br?nnimann wrote:
Quote:
Dear all

is the definition of Foo::bar_, which is a reference to
a forward declared class, well defined according to the standard?

Yes.

Quote:
My gut feel tells me that it's not and that it works in g++ by
accident only.

I wish I had accidents like that more...

Quote:
[...]

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bob Hairgrove
Guest





PostPosted: Sat Nov 20, 2004 6:19 pm    Post subject: Re: Reference to a forward declared class as class member? Reply with quote



On 19 Nov 2004 10:07:39 -0500, [email]broeni (AT) hotmail (DOT) com[/email] (Stephan Br?nnimann)
wrote:

Quote:
Dear all

is the definition of Foo::bar_, which is a reference to
a forward declared class, well defined according to the standard?

My gut feel tells me that it's not and that it works in g++ by
accident only.

foo.h
=====

class Bar; // in bar.h

class Foo {
public:
Foo(Bar& bar);
private:
Foo& operator=(const Foo&); // not implemented
private:
Bar& bar_;
};

foo.cc
======

#include "bar.h"

Foo::Foo(Bar& bar) : bar_(bar)
{
// empty
}


It is legal AFAICS. See the last example given in section 9.1.2 of the
standard, which uses references to forward-declared, but not defined,
classes.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

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