Jump to content
  • Advertisement
MarcusAseth

Constructor/Destructor not being called

This topic is 380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

 

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
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

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

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
}

 

Share this post


Link to post
Share on other sites

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

 

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!