Hi!

I am not too expert in C++ programming.
I created a class (Pig), then I created an object (Jack the Pig).
In the function Kill, I call the destructor, but it doesnt't "kill" (destroy) my Jack the Pig, because in the next line, I can feed him.

So my question is that why can't I destroy my object (Jack The Pig)?

Thanks for your help

#include "StdAfx.h"
#include <iostream>

class Pig
{
public:
	Pig();
	~Pig();
	void Feed(int f);
	void Kill();
private:
	int Weight;
};

Pig::Pig()
{
Weight=20;
}

Pig::~Pig()
{
std::cout << "Pig is killed!\n\n";
}

void Pig::Feed(int f)
{
Weight+=f;
std::cout << "The pig's weight is: " << Weight << "kg.\n";
}

void Pig::Kill()
{
	Pig::~Pig();
}


int main()
{
Pig Jack;
Jack.Feed(20);
Jack.Kill();
Jack.Feed(20);
return 0;
}

Try creating a new Pig ;)).First declare a pointer to a pig like this

Pig* Oliver

.Then assign a new Pig () to it like this.

Oliver=new Pig();

.When you're done feeding and killing him ;)) delete de poor pig from memory.

delete Oliver;

.And maybe you should join PETA ..

Calling the destructor does not erase the object. It is just like calling any other member function on the object. What makes the destructor special is that it gets called automatically when the object is deleted. An object is deleted when the memory it occupies is freed. In the case of a dynamically allocated object (heap-allocated) using the "new" operator, the object gets deleted when you call "delete" on its pointer. In the case of a stack object (like a local variable), the object gets deleted when it goes out-of-scope (the block in which it was declared ends). So, the code you tried to write or expected to have done is:

int main() {
  {                // open a new enclosing block.
    Pig Jack;      // create a stack-based Pig object.
    Jack.Feed(20); // feed Jack the Pig.
  };               // Jack goes out-of-scope here, its destructor is automatically called.
  Jack.Feed(20);   // ERROR! 'Jack' was not declared in this scope.
  return 0;
};

Calling the destructor explicitly is not something you do normally. The destructor is always called when the object's life-time ends, but the destructor does not end the object's life-time, you just understood the causality relation in the wrong direction. Generally, an object has no control over its life-time, it is always the code (or class) that creates the object that determines when it gets destroyed (at least, that's how it should be). In C++, the code that creates the object, owns the object and is responsible for destroying it when appropriate. The object itself cannot take ownership of itself and decide of its own faith. In other words, if you own a Pig, you decide when to make pork-chops with it, not the Pig.

This is different from Java or C# where nobody controls when objects get created or destroyed (because of garbage collection and reference-semantics) which implies that you cannot do any meaningful, predictable code upon construction / destruction, which, in turn, makes RAII impossible, which, in turn, makes these languages pretty bad in my opinion.

Thank you for everybody.

And thank you very much for mike_2000_17, your answer was absolutely perfect. :)
Now, I understand the whole thing, and the casuakity relation.

This article has been dead for over six months. Start a new discussion instead.