Class within a class

I am trying to create a class in a class, and call functions and variables from the subclass. Here is the code I have so far:

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
#include <iostream>
#ifndef _MAIN_CPP_
#define _MAIN_CPP_

class subclass
{
    unsigned int integer[];
};

class mainclass
{
public:
    unsigned int integerlength=5;
    mainclass()
    {
        subclass inttest(integer={1,2,3,4,5});
    }
};

int main()
{
    mainclass classtest;
    for(int i=0;i<mainclass.integerlength;i++)
    {
        std::cout << classtest.inttest.integer[i];
    }
    return 0;
}

#endif 
What help you need ?
Well, just a couple of things you did wrong here.
1). You are using two separate classes, not one inside the other
2). You are (incorrectly) creating a temporary variable inside the constructor.

I think you are looking for something like this:

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
#include <iostream>

using namespace std;

class mainclass
{
public:
    unsigned int integerlength;
    class subclass
    {
    public:
        unsigned int integer[5];
        subclass()
        {
              integer[] = {1,2,3,4,5};
        }
    }inttest;
    mainclass()
    {
        integerlength = 5;
    }
};

int main()
{
    mainclass * classtest = new mainclass();
    for(int i=0;i<classtest->integerlength;i++)
    {
        std::cout << classtest->inttest->integer[i];
    }
    return 0;
}


PS I didn't test this code, and I'm tired so there is most certainly something wrong with it, but it should point you in the right direction (more or less)
but is there any way I can have subclass outside of mainclass so I can reuse it?
Put your class declarations into a header file, and the definitions of their functions into a corresponding .cpp file. To use a variable of a particular type, include it's header file.
Okay I adjusted the code and took into consideration what was said and I got this:

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
#include <iostream>
#ifndef _MAIN_CPP_
#define _MAIN_CPP_

class subclass
{
public:
    //unsigned int integer[];
};

class mainclass
{
public:
    unsigned int integerlength;
    mainclass()
    {
        integerlength=5;
        subclass inttest;
    }
};

int main()
{
    mainclass classtest;
    for(int i=0;i<classtest.integerlength;i++)
    {
        //std::cout << classtest->inttest->integer[i];
    }
    return 0;
}

#endif 


inttest will initialize, but when I try to use a variable it doesn't work. Anyone know how I can fix this?
Your inttest object only exists within the scope of the constructor. As soon as the program exits the constructor, inttest is destroyed, so it never exists. What you probably want is to store the object as one of mainclass' member variables.

1
2
3
4
5
6
7
8
9
10
class subclass{/*...*/}object;
class mainclass{
   public:
      unsigned int integerlength;
      subclass *inttest;  //<-- This is valid code.
      mainclass(){
         integerlength = 5;
         inttest = &object;
      }
};


Edit:
Take my example with a grain of salt as I realize that I don't really understand how you want to implement your class within a class, e.g. if you want to create a pointer to the subclass or if you want to create a nested class as other above have mentioned.
Last edited on
Ok now one last question, I did what you said but how can I change the array integer?
Topic archived. No new replies allowed.