I have a dilemma. I have a main that is calling a template class. The main creates an object of some template class called X with an int and an array of char, such as a(4) for int and v("blah") array of char. This templated class creates an object of another templated class calling its default constructor. This default constructor is supposed to set all of its variables to null. However, from what I understand an array of char does not default to NULL but '/0'. So i have a conflict. The int wants a NULL and the char wants a '/0'. Any ideas? I know this is confusing so ill try to paraphrase it below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
main(){
A<int> a(4);
A<char> b("blah")
}
template<class X>
class A{}
something{
B();
}
template<class Y>
class B{
public:
Y var;
B::B(){
var = NULL;
//or var = /0;
}
First, I would suggest using an initializer list instead of a constructor like that.
Anyway, the default constructor of most things that do not normally have default constructors (like ints) just set them to 0. I'm not sure about char arrays, but they might be set to 0 (NULL) as well.
Btw, A<char> b("blah") will not compile. You could use char*, but std::string is MUCH better.
My real program setup actually does have an intializer list and it does pretty much the same thing. The problem is when i initialize var to Null my a(4), or int, works perfectly fine. However, when the b("blah"), or char*(thanks it should be char* not char), runs through the program and sees the NULL the compiler is confused and thinks I'm trying to turn a char* into an int. I'm guessing its looking for a '/0'. I don't know if there's some kind of way to overload this default constructor so that when an int passes to it it sets the variable to NULL and when a char* passes to it it sets the variable to '/0'. Any thoughts are greatly appreciated!
Don't set it to anything, just call the default constructor like this:
1 2 3 4 5 6 7
template <class T>
class Temp {
public:
T var;
Temp() : var() {} //this isn't even really necessary, since var is the only variables
//and the default constructor does this anyways
};
EDIT: If you have to set it to something special for certain types (don't know why I didn't think of this XP), use template specialization (go look it up if you don't know how).
Can you post a more complete example of what you are trying to do (your original post isn't
even close to compiling)? I cannot understand from your description, and I think there might be
some inaccuracies in some of the responses.
Thanks a bunch for the help. When I had the empty parentheses it was able to accept both an integer and a char*. Greatly appreciate the help! I'm looking into the template specialization to work out other problems with my program.