Class within a Class

I don't understand why this first bit of code works, and not the second. I like the look of the second code better, is this the most efficient way of doing this?

1
2
3
4
5
6
7
8
9
10
11
class STATS {
  public:
    int value1;
    int value2;
    class PLAYERS {
      public:
        int value3;
        int value4;
        char name[50];
    } play1, play2, play3;
};


That will compile. This will not.

1
2
3
4
5
6
7
8
9
10
11
12
13
class PLAYERS {
  public:
    int value3;
    int value4;
    char name[50];
};

class STATS {
  public:
    int value1;
    int value2;
    PLAYERS *play1, *play2, *play3;
};


I think the latter is much easier to read, unless there's a better way to write this. Also... here's the rest of the functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void setvalues( STATS *test )
{
    cout << "What is your name: ";
    gets( test->play1.name );
}
 
int main(int argc, char *argv[])
{
    STATS myclass;
    
    setvalues( &myclass );
    cout << myclass.play1.name << endl; 
    return 0;
}
The problem is not the class declaration.

On your second snippet, you declaring play1, play2, and play3 as pointers, but on your first snipper, you're declaring them as objects.
On your third snippet, lines 4 and 12, you're accessing play1 as an object, that will cause a compiler error if you use your second version.

Remember that pointers must be made to point to something valid, or to zero. Dereferencing invalid pointers has undefined behavior (anything could potentially happen). What usually happens is that the program crashes with a segmentation fault.
I've taken a look at the tutorial and am still a little fuzzy on how pointers work outside of just declaring them and refrencing them between functions. I am having a hard time grasping pointers with classes. What would be the proper way to do the snippets I have above so that they are pointers where I can modify their data in any function?
To dereference a pointer you do this: *pointer
To access the members of an object, you do this: object.member
A dereferenced pointer is an object, therefore: (*pointer).member
The shorthand of which is: pointer->member
Ok, now it compiles fine, but it's crashing. Here's a slimmed down version of what I'm working with. The pointer is access the int variable age, although it's not allowing it to be written... What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct PLAYERS {
    int age;
    char name[50];
};
       
struct ITEMS {
    PLAYERS *player1;
    PLAYERS *player2;
    int variable1; 
};

int main( )
{
    ITEMS game;
    game.player1->age = 10;
    return 0;
}
Remember that pointers must be made to point to something valid, or to zero. Dereferencing invalid pointers has undefined behavior (anything could potentially happen). What usually happens is that the program crashes with a segmentation fault.

You haven't made your pointers to point to anything valid. You need a constructor for ITEMS that does that.
For example: this->player1=new PLAYERS();
Topic archived. No new replies allowed.