| View previous topic :: View next topic |
| Author |
Message |
hongseok.yoon@gmail.com Guest
|
Posted: Mon Sep 12, 2005 9:33 am Post subject: Can I define predeclaration of inner class? |
|
|
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
|
Posted: Mon Sep 12, 2005 2:46 pm Post subject: Re: Can I define predeclaration of inner class? |
|
|
[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
|
Posted: Mon Sep 12, 2005 2:49 pm Post subject: Re: Can I define predeclaration of inner class? |
|
|
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
|
Posted: Mon Sep 12, 2005 5:26 pm Post subject: Re: Can I define predeclaration of inner class? |
|
|
<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
|
Posted: Tue Sep 13, 2005 9:06 am Post subject: Re: Can I define predeclaration of inner class? |
|
|
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 |
|
 |
|