To answer your first question: Using
new to instantiate a class dynamically allocates memory for an object of that class on the heap. The value that is returned from this operation is a
pointer to the memory location for that instance. Hence, the compiler is complaining that you're trying to assign a pointer value to a non-pointer variable
c (of type
Person). To fix that, simply change line 21 to:
Person * c = new Person();
(Note the * symbol)
Also note that you should free this memory using the
delete keyword when you're finished using that object.
To attempt to answer your second question (and I stress that there's probably a better way to do it!): When I read this, I thought that the
Singleton pattern might be useful here. You could create a class to store a random number, and this class can only be instantiated once (i.e. a singleton). You could pass a reference to this object to each of your Person instances that you create. Then, each of your Person instances will have access to the same random number object.
Explanation of what the Singleton pattern is:
http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B
@More experienced posters: Please feel free to jump on this suggestion...I'd like to know if it's overkill.
Hope it helps.