Public Group

Constructor/Destructor not being called

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?

#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 on other sites

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

Try "Animal seal;"  (no parentheses)

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

Edited by MarcusAseth

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 on other sites

Got it, sorry for doubting

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?

Edited by MarcusAseth

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 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 on other sites

Got it    Thank you both!

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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631782
• Total Posts
3002324
×