Re:Dynamic allocation of arrays.
"JB" <
XXXX@XXXXX.COM >writes:
Quote
Thanks for each of the responses to this. I was a C programmer
in the old days and have transferred to C++. I did not know
about the container classes. I spent today getting to grips
with them - should be able to do some good stuff with them.
The help text says that if a user defined element is used, it
must have a copy constructor. Am I right in thinking that this
is a constructor for the class which takes a pointer to an
object of it's own type, as the parameter and creates the new
object as a direct copy of the object passed to it? Hope I
have explained this OK.
Almost. A copy constructor takes a reference to another instance,
which is used as a prototype for how to create this new object.
Unless you have very unusual requirements, the signature of the copy
constructor is:
class x
{
public:
x(x const & other); // copy constructor
};
While a pointer and reference are very similar, the easiest way to
seperate them is by thinking "references use a dot, pointers use an
arrow." There is, of course more difference, but that is
syntactically the difference.
Other differences include:
* pointers can be null, references must be initialized to a valid
object
* pointers can be changed, references cannot be re-bound
* pointers need to be explicitly dereferenced, references do not
I'm probably missing other aspects, but for a quick off-the-cuff list,
this is probably enough to get you going.
--
Chris (TeamB);