i used the deletename() to clear the existing heap before reassignment, it that okay? |
Calling deletename() will prevent a memory leak, but that isn't what I meant. Think about what happens if you declare a
Student s
and then do
s = s;
. It calls your assignment operator with
this
and
s
refering to the
same object. You call deletename(), which clears name. Then line 37 executes
name = new char[strlen(s.name)+1];
. But since
this
and
s
are the same object, that means that s.name is NULL. So
strlen
will fail and the program will probably crash.
By the way, since name is allocated as an array, deletename should call
delete [] name
instead of
delete name
.
cstring is the new edition of strings.h |
Correct.
wat do u mean by string literals |
A string literal is an expression like "hello world". The type of such an expression is
const char *
3. the program is now a little bit slower tho.... |
On my system it takes about 15ms to load and run.
Finally, you don't need to create Students on the heap in main. Lines 77-80 should probably be:
1 2 3 4
|
students.push_back(Student("Papi John", 003));
students.push_back(Student("James Bond", 007));
students.push_back(Student("Peter Parker", 001));
students.push_back(Student("Alan Shearer", 90));
|