-3
class A { int* a; };
class B : public A { int*b; };

int main() {
    A* ptr = new B();
    delete ptr;
}

class A is a pure virtual interface class and class B inherits from class A. When we delete ptr which destructor will be called? The one from the A class or the one from from the B class?

adentinger
  • 1,367
  • 1
  • 14
  • 30
matzar
  • 287
  • 2
  • 19
  • 2
    What do you mean by "pure virtual interface class", and why do you think A is one? – user2357112 Apr 24 '17 at 19:11
  • see also: http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – UnholySheep Apr 24 '17 at 19:12
  • 2
    The one from `A`, which is why classes meant to be base classes for runtime-polymorphic objects should declare a virtual destructor (`virtual ~A() { }`). – cdhowie Apr 24 '17 at 19:12
  • please provide some meaningful code (ie one that can compile). I fixed one typo (`Delete`->`delete`), but the `ptr->print();` you need to fix – 463035818_is_not_an_ai Apr 24 '17 at 19:12
  • 2
    Your code as presented, is both non-compilable and unclear. Post a proper [mcve]. – StoryTeller - Unslander Monica Apr 24 '17 at 19:12
  • It's not clear in the example and it should be more specific but it's more like a pseudo-code example so I don't really know what you mean? Class A is a virtual class because it contains virtual function and is an interface for Class B? But you're right, the example doesn't state it clearly but the point of the question is a bit different I believe. – matzar Apr 24 '17 at 19:14
  • It's not for us to divine the point of your question from a description of code that is completely unrelated to the code you presented. Be clear. – StoryTeller - Unslander Monica Apr 24 '17 at 19:15
  • Without seeing exactly how you define your classes, the question can't be answered. – Anon Mail Apr 24 '17 at 19:16
  • The code was probably unnecessary. I hope the question is clear enough on it's own. – matzar Apr 24 '17 at 19:16
  • The question was given to me during an interview so no code should be needed. – matzar Apr 24 '17 at 19:17
  • 2
    Possible duplicate of [When to use virtual destructors?](http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors) – matzar Apr 24 '17 at 20:05

1 Answers1

3

Comment: First of, why do you have code outside any function? Statements only make sense when there are within the body of a function, like main.

Assuming the statements you posted were supposed to go into main:

Answer: delete ptr will call the destructor of A. The compiler will not 'think' any further than this.

Reason: All methods (including the destructor) are non-virtual by default. In your case, you did not specify that the destructor should be virtual. The compiler sees that you are calling the destructor on a A* pointer, so it calls the destructor of A.


What if I had specified that Class A destructor was virtual? Would it still call the destructor of Class A?

Answer: If it were virtual, it would call the destructor of B, because the actual type of the object would be determined during the execution of the program.

See more about virtual functions and polymorphism here.

adentinger
  • 1,367
  • 1
  • 14
  • 30
  • What if I had specified that Class A destructor was virtual? Would it still call the destructor of Class A? – matzar Apr 24 '17 at 19:19
  • Done ; see edit to my post. Please post the code back though ; my answer makes no sense without it. just put the statements inside a `main` function. – adentinger Apr 24 '17 at 19:21
  • Thank you. I believe the code was sufficient as you had answered my question. It could have benefited from putting into mail - definitely. But I think I'll delete the question since it got so many downvotes. – matzar Apr 24 '17 at 19:26
  • @carrotcake Okay. Good luck learning c++! – adentinger Apr 24 '17 at 19:27
  • I've posted the code back. But you answer is really good so I'll keep it somewhere for the future reference for myself. And the community seems to not like it. Don't know why. – matzar Apr 24 '17 at 19:30
  • @carrotcake There were formatting problems in your post. Since you want others to make the effort to answer your question, *you* should make the effort to make your question easy and quick to understand. People are helping you for free after all. – adentinger Apr 24 '17 at 19:35