I am writing a program that uses a class with dynamically allocated data member in it.
In the class constructor for the program, I get errors that the variables are not used.
void example()
{
char *name = NULL;
int value = 0;
}
What does this function do with these two variables: name and value?
Nothing.
Those are local automatic variables that are created within the body of the function and automatically destroyed at the end of the body of the same function.
What does change, if we leave them out?
1 2 3
void example()
{
}
Nothing.
Your class may very well have some member variables that you do use, but these are not those variables.
Warnings aren't as big of an issue as errors. They aren't the same as errors.
The reason you are getting a warning is because name and value are local variables to the scope of the constructor. Since they are local variables, they will stop existing after the constructor is completed.
What is your purpose in doing this? Do you have data members of Class called name and value as well? If so, this is actually WRONG. If you want to set the data members, you have to do this:
1 2 3 4 5
Class::Class()
{
name = nullptr; //Use nullptr instead of NULL if your compiler supports it
value = 0;
}
Or better yet, get rid of the default constructor and simply initialize the data members with the values directly.
Class::Class()
{
char *name = NULL;
int value = 0;
}
Is this your constructor? Does your class already have member variables char* and int?
It looks like you are defining them again in your constructor. thy this...
What? Warnings are issues that should be fixed, especially when learning the language.
My bad. I meant to say that warnings are not the same as errors. OP seems to be equating the two (although you can set your compiler to treat warnings as errors).