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 

Can I define predeclaration of inner class?

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





PostPosted: Mon Sep 12, 2005 9:33 am    Post subject: Can I define predeclaration of inner class? Reply with quote



I can predeclare of my class A like bellow.

class A;

Then, can I predeclare of my inner class B of class A?

class A
{
public:
class B
{};
};

If possible, how can I?


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

Back to top
Daniel Krügler
Guest





PostPosted: Mon Sep 12, 2005 2:46 pm    Post subject: Re: Can I define predeclaration of inner class? Reply with quote



[email]hongseok.yoon (AT) gmail (DOT) com[/email] wrote:
Quote:
I can predeclare of my class A like bellow.

class A;

Then, can I predeclare of my inner class B of class A?

class A
{
public:
class B
{};
};

If possible, how can I?

Declare like ordinary classes, but inside:

class A
{
public:
class B;
};

Define later:

class A::B {
...
};

Note: You can't predeclare A::B without **defining** A, i.e. the
following is illformed:

class A; // OK
class A::B; // Not OK

Greetings from Bremen,

Daniel Krügler

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


Back to top
Tony Delroy
Guest





PostPosted: Mon Sep 12, 2005 2:49 pm    Post subject: Re: Can I define predeclaration of inner class? Reply with quote



You can use...
class A
{
public:
class B; // declaration

// other code the uses B by reference or pointer

class B // definition
{
// benefits from code between B's declaration and definition
};
};

AFAIK, the full definition of inner classes has to be completed within
the class definition.

Tony


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

Back to top
Ben Hutchings
Guest





PostPosted: Mon Sep 12, 2005 5:26 pm    Post subject: Re: Can I define predeclaration of inner class? Reply with quote

<hongseok.yoon (AT) gmail (DOT) com> wrote:
Quote:
I can predeclare of my class A like bellow.

class A;

Then, can I predeclare of my inner class B of class A?

class A
{
public:
class B
{};
};

If possible, how can I?

You define class A like this:

class A
{
public:
class B;
...
};

Then you define class A::B separately like this:

class A::B
{
...
};

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

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


Back to top
Daniel Krügler
Guest





PostPosted: Tue Sep 13, 2005 9:06 am    Post subject: Re: Can I define predeclaration of inner class? Reply with quote

Tony Delroy wrote:
Quote:
You can use...
class A
{
public:
class B; // declaration

// other code the uses B by reference or pointer

class B // definition
{
// benefits from code between B's declaration and definition
};
};

AFAIK, the full definition of inner classes has to be completed within
the class definition.

No, you can define the inner class at another place, e.g in your
implementation file as long as you don't try to access members of B in
the definition of class A previous to the point of definition of A::B,
see Our Holy Standard 9.7/p.3:

"If class X is defined in a namespace scope, a nested class Y may be
declared in class X and later defined in the definition of class X or be
later defined in a namespace scope enclosing the definition of class X.

[Example:
class E {
class I1; // forward declaration of nested class
class I2;
class I1 {}; // definition of nested class
};
class E::I2 {}; // definition of nested class
—end example]"

Greetings from Bremen,

Daniel Krügler

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