And for testing purposes you can easily create a block with just { and }
Code:int main() {
cout << "hello\n";
{
Dog rex;
rex.setWeight(23);
cout << rex.getWeight();
}
// rex ceased to exist from here on.
}
Printable View
And for testing purposes you can easily create a block with just { and }
Code:int main() {
cout << "hello\n";
{
Dog rex;
rex.setWeight(23);
cout << rex.getWeight();
}
// rex ceased to exist from here on.
}
Actually, it doesn't matter. But thanks anyway.
A reset function is enough.
It makes it able to reset the name, the color and the weight.
Actually, every integer has a value. It cannot be "Deleted".
Actually, you reset the name to 0 = "" as a string, same for the color. But you reset the weight to 0 mean, the weight doesn't even have a real value. That's how programs work. Normally, in a game, when you got weight, let's say in inventorys, it will show 0/total weight. it cannot show nothing/totalweight, this will mean that the weight doesn't exist.
Well, I'm glad you have all the answers after all. Even if the wrong ones. ;)
So, if you set a dog weight to 0 it will disappear? Maybe in the real world if you somehow managed such a feat. But you are in the realm of programming. In here things function a little differently.Quote:
Normally, in a game, when you got weight, let's say in inventorys, it will show 0/total weight. it cannot show nothing/totalweight, this will mean that the weight doesn't exist.
An object only ceases to exist, as others have told you, when you reach the end of its scope. Until then, setting some property to 0 will not make the object disappear. In C++ an object lifetime is a fundamental concept taught right in the early stages as you are studying variables, their scopes and lifetimes. You should go back to that part.
If you want to make a stack instance of Dog disappear, you need to go past its scope. Naturally you can also set a property of Dog that indicates that object is no longer valid. But you should code with meaning in mind. There's no meaning to a dog with getWeight() == 0. But certainly there could be to a isValid() = true.
Nonetheless that's generally bad practice. If your code doesn't need to use an object anymore, you don't need to access it and it will eventually fall out of scope.
C++ isn't typesafe, not native C++ anyway.
In C#, were you to actually dipose of an object or close it or anything similar, you would in fact get an error - or as they do it in .NET, an exception would be thrown.
However, what happens when you free memory or destroy an object is completely dependent upon the compiler and perhaps the implementation of the memory allocation functions.
Take this code, for example:
In the majority of cases, the second value will print 5 before and after free. Most implementations of memory allocation algorithms such as the malloc one simply mark the memory as unused - they don't necessarily zero it out or anything similar.Code:int main(void)
{
int *iSomething = malloc(sizeof(int));
*iSomething = 5;
printf("iSomething before free = %d\n", *iSomething);
free(iSomething);
printf("iSomething after free = %d\n", iSomething);
return 0;
}
This applies to C++'s "new" operator too(Which is, IIRC, just a wrapper for malloc, someone correct me if I'm wrong). This is why it's often called good practice to NULL out a pointer that's been freed to make sure you get an error the first time you use it after the free, if you accidentally forget you did indeed free it. Otherwise, it might just result in a bug MUCH later in the code which will be extremely hard to track down.
I did not say, if you set the weight to 0 , it disapears,...it just sets it to 0...mean the value will be 0...And, a dog will never have a weight of 0...it means that in this case, deleting the weight and making it invisible isn't really important, in this case,why not just not use weight? if you want to add the weight, then, it must have a value, otherwise, you don't need it...
And what i just said now is not wrong.
Hehe....i already understood that, but you didn't know what i was trying to say, i was asking if there was a way to do it...but i had no point of asking for a way to do it...haha...thanks.