Question: C++ I have a factory method in my base class which returns a class pointer to one of two derived classes. I'm supposed to create

C++

I have a factory method in my base class which returns a class pointer to one of two derived classes. I'm supposed to create a virtual destructor. My question is what is this virtual destructor supposed to do. Here is the code for my factory method (inside the base class).

C++ I have a factory method in my base class which returns

My previous lesson didn't deal with pointers so my previous destructor was just an empty destructor. Now, since the new method is a factory method and deals with pointers, I need a virtual destructor. How is that different than a regular destructor and this time I'm assuming I need to add something to it, instead of just an empty destructor.

Pet *Pet::create(string filename) f ifstream ifs(filename); string line getline(ifs, line) ifs.close); if (line"Cat") return new Cat(filename); if(line = "Dog" ) return new Dog(filename); return nullptr

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!