data member of arrays of another class

New at programming and need a bit of guidance...

If I have two classes, let's say

class Pink
and
class Yellow

and I want to create an array as a data member in class Yellow that point to class pink. What will my data remember of array format look like.

class Yellow
{
int size = 20;
Pink array[size];
int *pointTo = &Pink;

--------
}

Putting everything together is a bit confusing.

Thanks!
Last edited on
Two problems:

1) size must be a const.

2) pointTo can not be initialized in the declaration. Initialize it in your constructor.


Thanks!!
can I do
Item array[100];
instead of making a constant.

Also, could you please explain why pointTo needs to be initialized in the constructor?
Last edited on

can I do
Item array[100];

Yes.

could you please explain why pointTo needs to be initialized in the constructor?

Since array has not been allocated in memory yet, you can't take it's address. array only exists in memory when Yellow is instantiated.

1
2
Pink array[size]; 
int *pointTo = &Pink;

BTW, you can't take the address of a type (Pink). You can only take the address of an object that exists in memory. Types tell the compile what something looks like, but don't occupy memory until they are instantiated.


thanks!!
Topic archived. No new replies allowed.