• Advertisement

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By nickyc95
      Hi.
      I'm kind of late to this party but I thought I would ask anyway as I haven't found a concrete answer.
       
      When creating a game engine, when should you choose one methodology over another (more specifically OOP and DOD)? Which areas benefit from DOD? Which areas benefit from OOP? Do people typically mix multiple methodologies throughout a project? I.e. certain sub-systems created in one, and others in the another?  
      DOD - Data Oriented Design
      OOP - Object Oriented Design
       
      Pretty simple
      Thanks
    • By Estra
      Memory Trees is a PC game and Life+Farming simulation game. Harvest Moon and Rune Factory , the game will be quite big. I believe that this will take a long time to finish
      Looking for
      Programmer
      1 experience using Unity/C++
      2 have a portfolio of Programmer
      3 like RPG game ( Rune rune factory / zelda series / FF series )
      4 Have responsibility + Time Management
      and friendly easy working with others Programmer willing to use Skype for communication with team please E-mail me if you're interested
      Split %: Revenue share. We can discuss. Fully Funded servers and contents
      and friendly easy working with others willing to use Skype for communication with team please E-mail me if you're interested
      we can talk more detail in Estherfanworld@gmail.com Don't comment here
      Thank you so much for reading
      More about our game
      Memory Trees : forget me not

      Thank you so much for reading
      Ps.Please make sure that you have unity skill and Have responsibility + Time Management,
      because If not it will waste time not one but both of us
       

    • By mike44
      typedef struct FontInfo_t* FontHandle;
      MichaelNS::FontMgr m;
      MichaelNS::FontHandle FontGeladen;
      const char *FontPtr;
      start:
      InFileFont.append("Ubuntu-B.ttf");
      FontPtr = InFileFont.c_str();
      FontGeladen = m.loadFont(FontPtr, NULL, NULL, 14, 0);
      stop:
          m.unloadFont(FontGeladen);
          delete FontPtr;
      delete causes a crash. How to unload the class correctly?
      Thanks
    • By Cat's machete
      I need to use global vars,
      So say i have main.cpp file where i #include "globalvars.h"
      In globalvars.h i define
      extern int ppp;
      In globalvars.cpp i put #include "globalvars.h"
      int ppp;
       
       
      Now in main cpp during compilation i get UNDEFINED REFERENCE TO ppp
  • Advertisement