Click here to Skip to main content
15,847,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello all,
I'm having a hard time using nested classes. most examples look like
C++
class A
{
    public: 
    private:

   class B{
      public:
      private:
}
}

However the program I have been working on requires me to implement a nested class that looks like the following

C++
class A
{
public: //some functions
private:  class B
         {
           public:  string s;
                    int x;
         }//ends the nested class
string s2;
int x2;
}//ends outer class


OK! so now that I've given a model of what I'm working with
my questions are:
1. do I still make a constructor for class B?
would that look like ....
C++
class A:: class A( ) {                //constructor for A
   class A:: class B:: class B( ) {     //constructor for B

    }


by the way that looks confusing
OR.....

C++
class A ::class A(){   }          //class A constructor
   class A:: Class B :: class B (){} // class B constructor outside of A's constructor

2. how do I use the variables for class B?
do I always have to specify where they are?
for example
C++
type  class A ::class A function(){
                x2= x+ 5;
               return x2;
     }


x2 belongs to A, but will the compiler understand that x belongs to B?

What I have tried:

to be honest my first attempt was typed like the following
C++
class A :: class A( ){
   class A::class B :: class B(){  }
}

I had a gut feeling that it's completely wrong. I haven't debugged yet because I'm still writing the rest of the functions for class A
Posted
Updated 25-Nov-17 5:11am
v2

Do you mean something similar to
C++
#include <string>
#include <iostream>
using namespace std;

class A
{
public: //some functions
  A(int x, string s, int x2, string s2);

private:
  class B
  {
  public:
    B(int x, string s);
    string s;
    int x;
  };//ends the nested class

  B b;
  string s2;
  int x2;

public:
  string getbs();


};//ends outer class


A::A(int x, string s, int x2, string s2):b(x,s),s2(s2),x2(x2)
{
}

A::B::B(int x, string s):s(s), x(x)
{
}

string A::getbs(){return b.s;}

int main()
{
  A a(1,"hi",2,"hello");

  cout << a.getbs() << endl;
}
?

Please note, due to access rules (B is a private in A) you cannot access A::B in the main function.
 
Share this answer
 
Comments
Laxmidhar tatwa technologies 27-Nov-17 7:49am    
A good usefull code
 
Share this answer
 
Comments
Member 13540005 25-Nov-17 14:23pm    
thank you, that article helped a lot. One last question, with nested classes , it doesn't matter where the other classes are? I'm asking because my nested class is a private variable...then does advice in the article still holds true regardless?
Richard MacCutchan 26-Nov-17 2:57am    
Sorry, I am not sure I understand the question. The nested class has the same visibility rules as any other member, so it can be private to the enclosing class, or public to the code in which the enclosing class is used.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900