Connecting Tech Pros Worldwide Help | Site Map

can a class definition inside another class's definition

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 01:59 AM
Jianli Shen
Guest
 
Posts: n/a
Default can a class definition inside another class's definition

Hi,

I want to implement like this:

class A{

int length;

class B{
void operate_length_in_A(){
length++;
}

void use_length_in_A(){
if(length>8){
}
}

B *Bobj;

}

because B will use member in A, so I think I can not put B ahead of
definition of A.

How can I do?

Thanks



  #2  
Old July 23rd, 2005, 01:59 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: can a class definition inside another class's definition

"Jianli Shen" <jianli@cc.gatech.edu> wrote...[color=blue]
> I want to implement like this:
>
> class A{
>
> int length;
>
> class B{
> void operate_length_in_A(){
> length++;
> }
>
> void use_length_in_A(){
> if(length>8){
> }
> }
>
> B *Bobj;
>
> }
>
> because B will use member in A, so I think I can not put B ahead of
> definition of A.
>
> How can I do?[/color]

Contrary to Java where a nested class is automatically instantiated as
a member of the outer class and also gets all members of the outer class
accessible and associated with the same instance of the outer class as
the nested instance, in C++ a nested class definition is nothing but
a definition of a type.

If you need to access non-static data members of the outer class (like
'length' in your example) from a function that is not a non-static member
of that outer class, you would need an instance to go along with it:

...
class B {
void operate_length_in_A(A& a) {
a.length++;
}
...

Where you put the definition of B inside A shouldn't matter.

V


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.