Advertisement

Constructor/Destructor not being called

Started by September 09, 2017 06:12 AM
8 comments, last by Kylotan 7 years, 2 months ago

I just wanted to do some experiment with inheritance and virtual destructor, though before that I tested the base class with the code below, and noticed that no output appears in the console.

Why is that? Is the compiler being super smart about it? :S


#include <iostream>
using namespace std;

class Animal {
public:
	Animal() {
		cout << "Animal Constructor Called" << endl;
	};
	~Animal() {
		cout << "Animal Destructor Called" << endl;
	};
};

int main()
{

	{
		Animal Seal();
	}

	return 0;
}

 

You're declaring a function "Seal" taking no parameters, and returning an Animal.

Try "Animal seal;"  (no parentheses)

Advertisement
4 minutes ago, Alberth said:

You're declaring a function "Seal" taking no parameters, and returning an Animal.

Alberth what you says goes against what I read in other places, which is you cannot declare a function inside a function, and that line is into main(), so I think you are mistaken here.

Edit: though it works without parentheses by the way :D

Declaring can be done anywhere, defining a function is what you cannot do, ie

 


void f() {

  void g(); // declare allowed, it just says "there exists g()"

  void h() { ... } // error, nested definition
}

 

Got it, sorry for doubting :P 

By the way, there is any point in declaring a function inside a function ? Or is just something allowed and rather useless?

Meanwhile, a new questions arises:


#include <iostream>
#include <string>
using namespace std;

class Animal {
protected:
	string name;
public:
	Animal(string& n) :name{n} {
		cout << "Animal Constructor Called" << endl;
	};
	~Animal() {
		cout << "Animal Destructor Called" << endl;
	};
};

class Dog : public Animal {
public:
	Dog(string& n) :Animal(n) {
		cout << "Dog Constructor Called" << endl;
	};
	 ~Dog() {
		cout << "Dog Destructor Called" << endl;
	};
};

int main()
{

	{
		Dog MyDog(string("Spike"));
	}

	return 0;
}

Output:

Quote

Animal Constructor Called
Dog Constructor Called
Dog Destructor Called
Animal Destructor Called

 

I did't used a virtual Destructor on the base class, so why is my output correct? Is this the compiler being super smart? :D

 

You forgot to use Animal as type of MyDog  :)

The virtual destructor ensures the destructor always starts at the most derived class, and then crawls back up to the super classes.

Without it, it just starts at thetype that the data has at the time of the destructor call

Advertisement

You need virtual destructors when deletion is polymorphic.

e.g.


Animal* a = new Dog();
delete a;

Without a virtual destructor, that will result in:


Animal Constructor Called
Dog Constructor Called
Animal Destructor Called

This is a very bad bug to occur, so the general rule of thumb is that if a class contains any virtual methods (which indicates that it will be used polymorphicly), it should also have a virtual destructor.

edit: Alberth beat me :)

Got it :P   Thank you both! :) 

 

 

On 09/09/2017 at 7:34 AM, MarcusAseth said:

By the way, there is any point in declaring a function inside a function ? Or is just something allowed and rather useless?

Sometimes you want to use a function from another file but don't want to drag in a whole header for it.

Generally this would be frowned upon, because it's hidden the dependency inside the code instead of near the top of the file. A compromise would be to put the declaration near the #includes.

This topic is closed to new replies.

Advertisement