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;
}
Why can't I do it with a class I've defined, such as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class circle{
private:
int radius;
public:
circle(int rad){
radius = rad;}
};
circle red_circle = NULL;
int main(){
red_circle = circle(20);
return 0;
}
When I do this, it compiles but my compiler (Dev-C++) gives the warning: "passing NULL used for non-pointer converting 1 of `circle::circle(int)'"
I know this is a super-noob question and I appreciate you taking the time to help me.
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>?