I would like to declare an instance of a class, but set it equal to NULL and wait until later to initialize it with a constructor. This can certainly be done with a string as in the example below:
1 2 3 4 5 6 7 8 9 10
#include <string>
usingnamespace std;
string name = NULL;
int main(){
name = string("Billybob");
return 0;
}
You cannot assign a variable to the value null if it's of an object type. Only pointers can be set to null.
e.g
1 2 3 4 5 6 7 8
circle *red_circle = NULL;
int main() {
red_circle = new circle(20);
delete red_circle; // You have to delete the memory allocated with new
return 0;
}
Thanks for the info! That makes sense to me, but why don't you have to call "new" and subsequently "delete" when you're doing it with a string object? Are the new and delete calls built into the constructor/destructor?
EDIT: While trying to answer this problem myself I went looking for the source code for the C++ string class but was unable to find it. Does anyone know of a website that has the source code for any classes such as <string> <time> <iostream>?