nested class question

Hello,
Can someone please tell me how to access the member data and member functions from a class which is in the class of the class. I will be glad if anyone gives me an example .
Provided the nested class is public, you access it just like you would if it weren't nested. The only extra thing you need to do is put the host class with the :: operator (I forget the technical term it's called).

Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Host
{
public:
  class Nested
  {
  public:
    void PrintMe()
    {
      cout << "Printed!\n";
    }
  };
};

int main()
{
  Host::Nested foo;
  foo.PrintMe();


  Host bar;
   // nothing you can do with bar to call PrintMe
   //   Host::Nested and Host are two separate classes

  return 0;
}
Ok how will we access the data members and functions if there is a class in the class of a class I mean total three classes .
=P You can't figure it out? Maybe you shouldn't be nesting so many classes then.

Give me an example of what you want to do and what you've tried to make it work. Seems silly for me to have to make another example.
I dont have to make any program just want to clear my concepts of nested classes.
Yeah I was kind of a jerk -- sorry. ^^

It's all about the :: operator. A::B means that B is in A. So above, you use Host::Nested to get the nested class.

So -- think about it. If C is in B -- and B is in A... then:
1
2
3
4
5
// to get B:
A::B  b;

// so then to get C....
A::B::C c;
Ok Is the following Okay

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A 
( public :
int x;
float y;
class B {
A c;
}
}
int main  {

 B z;
  z.c.x=4;
 z.c.y=5;
return 0;
}


Last edited on
closed account (z05DSL3A)
No.

Apart from the syntax errors this is the code you posted:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class A 
{ 
public :
    int x;
    float y;
    class B 
    {
        A c;  
    };
};

int main()  
{
    B z;
    z.c.x=4;
    z.c.y=5;
    
    return 0;
}


The first thing you will likely get is an error on line 8, this will tell you that the class A is undefined.
The next thing that I can see is line 15, As Disch said you need to use scope resolution, so it would be: A::B z;
c is also a private ember of class B, so z.c... will fail.

If you can give a more details of what you are trying to achieve, it would be easier to help you.

Edit:
Here is a quick example of a nested class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

class Task 
{ 
public:
    class Error 
    {
    public :
        int code;
    };

    bool DoTask()
    {
        //...some task code that failed
        lastError.code = 666;
        return false;
    }

    Error getLastError(){return lastError;};
   
private:
    Error lastError;
};

//-----------------------------------------------------------------------------
int main()  
try
{
    using std::cout;
    using std::endl;

    Task aTask;

    if(!aTask.DoTask())
    {
        Task::Error e = aTask.getLastError();
        cout << e.code << endl;
    }
    
    return 0;
}
catch(...)
{
    std::cout << "Somthing whent wrong...big time." << std::endl;
    return 999;
}

/* End of file ***************************************************************/

Last edited on
sorry wolf !
I dont want to get confuse and make the mess of my program.
please dont mislead people here.
I can ask these type of stuff to make my program better.
Last edited on
closed account (z05DSL3A)
How exactly am I misleading you?

Your code is wrong, and messy, I just neatened it up and pointed out some errors in it.
Last edited on
@ wolf !

Do I have to find mistakes in this program too ??
As I did in your last program ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

class Task 
{ 
public:
    class Error 
    {
    public :
        int code;
    };

    bool DoTask()
    {
        //...some task code that failed
        lastError.code = 666;
        return false;
    }

    Error getLastError(){return lastError;};
   
private:
    Error lastError;
};

//-----------------------------------------------------------------------------
int main()  
try
{
    using std::cout;
    using std::endl;

    Task aTask;

    if(!aTask.DoTask())
    {
        Task::Error e = aTask.getLastError();
        cout << e.code << endl;
    }
    
    return 0;
}
catch(...)
{
    std::cout << "Somthing whent wrong...big time." << std::endl;
    return 999;
}

Last edited on
closed account (z05DSL3A)
Do I have to find mistakes in this program too ??

Please do.

I've probably been writing software longer than you have been shitting your pants, but I'm always willing to learn. So, grand master, please impart your wisdom.

I really don't understand why you ask for help and then ignore and insult the people that offer the help requested.
AT wolf !
Why the hell did you post that wrong program when I was learning classes last time ??
Last edited on
Wolf: Stop responding to him. He's a troll. And he has multiple accounts. (E.g., jayt)
closed account (z05DSL3A)
Masiht,

This will be my last post in this thread, unless you have questions pertaining to your problem. If you want to continue to discuss the merits of the answer you have received from me, please feel free to start a thread in the Lounge.

In answer to your question; Why the hell did you post that wrong program when I was learning classes last time ??
I did not post a 'wrong program', I posted code that you did not understand, there is a big difference.

Now we could go back to the original point of this thread, the code you posted:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A 
( public :
int x;
float y;
class B {
A c;
}
}
int main  {

 B z;
  z.c.x=4;
 z.c.y=5;
return 0;
}


Did you try compiling it? Found the syntax errors (opening parenthesis instead of a brace, the missing semi-colons)?
Did you then receive an error about undefined class A? and so on.


Hammurabi,

I am aware that he has multiple accounts, I subscribe to threads that I think I may need to follow up on, so get emails when anyone replies. Even in this thread he has posted as jayt, then quickly deleted it and posted as masiht. That dose not bother me.
Topic archived. No new replies allowed.