2

Code

#include<iostream>
struct A
{
    ~A()
    {
        std::cout<<"dctorA\n";
    }
};
struct B: public A
{
    ~B()
    {
        std::cout<<"dctorB\n";
    }
};

int main()
{
    B b1;
    b1.~A();
}

Output

dctorA
dctorB
dctorA

if destructor is not inheriting then how I am able to call it through object of B ?

and I know Rule of three and five but compiler work in spit of rule followed or not. so to avoid complexity I avoided writing copy constructor and overloaded= operator.

Abhishek Mane
  • 619
  • 7
  • 20
  • Where did you get the information from, that those aren't inherited? – Some programmer dude Jun 28 '21 at 06:08
  • @Someprogrammerdude I am reading book "let us C++" and writer is Indian. – Abhishek Mane Jun 28 '21 at 06:11
  • 1
    Well it's not entirely correct, and in fact mostly wrong. [Here's a list of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), I recommend you invest in one of them instead. – Some programmer dude Jun 28 '21 at 06:16
  • 1
    By the way, you should almost never all a destructor explicitly like you do with `b1.~A()`. Especially not a destructor of a parent class. – Some programmer dude Jun 28 '21 at 06:17
  • @Someprogrammerdude okay. – Abhishek Mane Jun 28 '21 at 06:23
  • @Someprogrammerdude and destructor un-initialize that memory of particular object and when lifetime of that particular object going to end then it calls destructor again but that object already destroyed so it leads undefined behavior so that's why we never call destructor right ? – Abhishek Mane Jun 28 '21 at 06:32
  • 1
    C++ Primer Plus is not to be confused with C++ Primer. The authors are different, and the former is viewed less favorably to the point that the Good Book List points this out. – sweenish Jun 28 '21 at 06:40
  • 1
    @AbhishekMane -- Remove the `b1.~A();`, as that is totally wrong and unnecessary. Now what is the issue you're having? – PaulMcKenzie Jun 28 '21 at 06:43

1 Answers1

2

What that statement is trying to say, is that class B does not automatically get a constructor B::B(int) when you add A::A(int). Similarly, B::operator=(int) is not automatically generated when you have A::operator=(int).

The copy constructor B::B(B const&) can be automatically generated, in which case it expects A::A(A const&). But you see from the argument that it's generated, not inherited as B::B(A const&).

The destructor works the same - generated, not inherited - but that's less visible because there's no parameter.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • first of all thanks for answering. I written constructor, copy constructor, overloaded = operator and destructor for base class and for derived class I didn't created anything. But all functionality working fine like constructor, copy constructor, overloaded = operator and destructor means compiler is generating them automatically right. This is contradicting to your first statement that constructor, overloaded = operator are not generated automatically for derived class if we write them manually in base class. – Abhishek Mane Jun 28 '21 at 17:41
  • please do reply. – Abhishek Mane Jul 04 '21 at 14:41
  • 1
    @AbhishekMane: I rechecked my original answer; it's correct. `operator=` like all functions can be overloaded, that's why I specifically gave `operator=(int)` as an example that is not automatically generated. Same for the converting constructor. – MSalters Jul 05 '21 at 06:56
  • Thanks I got it. one last `B::operator=(B)` is automatically generated right ? – Abhishek Mane Jul 05 '21 at 11:54
  • @AbhishekMane: Well, not exactly - that would take a `B` by value. That would be inconvenient; taking an argument by value requires a copy constructor. So the generated self-assignment takes a `B const&` and/or a `B&&`. – MSalters Jul 05 '21 at 11:57
  • I get it. but both `B const&` overloaded=operator and `B&&` Move overloaded=operator are automatically generated right ? – Abhishek Mane Jul 05 '21 at 12:01
  • 1
    @AbhishekMane: For details, check your book, but both are generated only if the bases classes and members would allow it. A non-moveable member will prevent move-assignment of the containing class. – MSalters Jul 05 '21 at 12:03
  • __1__ we can call constructor of base class explicitly through any member function of derived class like `base::base()` and what happen exactly is temporary object created and destroyed as that statement ends. But we can't call in such way from main function, similarly overloaded=operator __2.__ but we can't call copy constructor explicitly through member function of derived class we can just call indirectly by copying one base object to another in member function like `base b2=b1` if I try to do something like `base::base(b1)` it gives error . – Abhishek Mane Jul 06 '21 at 13:38
  • __3.__ Also destructor we can't call using membership label in member functions we have to use object. so what I believe in above 3 is right ? – Abhishek Mane Jul 06 '21 at 13:39
  • 1
    @AbhishekMane: If you've got a new question, don't put it in the comment section of an answer. Almost nobody sees it there. – MSalters Jul 06 '21 at 14:08