0

I'm kind of confused about how the reference works in the code below. From what I understand, b is just an alias of d1. So what's the difference between delete &b and delete d1 ?

b is of type Base but still is an alias of type f, so what differentiates them?

#include <iostream>

using namespace std;

class Base
{
public:
    Base(){
        cout << "Base Constructor Called\n";
    }
    ~Base(){
        cout << "Base Destructor called\n";
    }
};

class Derived1: public Base
{
public:
    Derived1(){
        cout << "Derived constructor called\n";
    }
    ~Derived1(){
        cout << "Derived destructor called\n";
    }
};

int main()
{
    Derived1 *d1 = new Derived1();
    Base &b = *d1;
    delete &b;
}
Sydney.Ka
  • 175
  • 7
  • 1
    `Base b = *d1;` creates a *copy* and `delete &b;` invokes *undefined behavior* - you are trying to `delete` an object with automatic storage duration – UnholySheep Dec 29 '19 at 00:10
  • 1
    And you probably want to read about `virtual` destructors: https://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – UnholySheep Dec 29 '19 at 00:12
  • Sorry I made a mistake. I meant Base &b = *d1 – Sydney.Ka Dec 29 '19 at 00:29
  • 1
    Now you still have *undefined behavior* because the destructor is not `virtual` (as described in the question I linked in my second comment) – UnholySheep Dec 29 '19 at 00:31
  • Yes, I know about virtual destructors (in this case I have to declare the destructor of ```Derived1``` virtual). In my case, I want to understand the difference between calling delete &b and delete d1 (especially what happens to the memory allocated) – Sydney.Ka Dec 29 '19 at 00:35
  • When we call delete &b (is the space allocated for the Base class is deleted and are vtable pointers of both the Base class and derived1 also deleted? ) – Sydney.Ka Dec 29 '19 at 00:38
  • You're fixating on implementation details that may or may not be in play, particularly given the undefined behaviour. I recommend not doing that. – Lightness Races in Orbit Dec 29 '19 at 00:52

1 Answers1

2

They are differentiated by type.

  • b refers to an object of type Base.

  • d1 points to an object of type Derived1.

Those are related, but distinct types.

Your object is a Derived1, not just a Base. This matters to delete. Make your destructors virtual and you'll get away with it, because that's how polymorphism works. Otherwise, you have undefined behaviour and there is no meaning to your program.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • From what I understand d1 is a pointer to an object that contains a Base class object and a vtable pointer (if we have also virtual functions). And b will contain nothing or only vtable pointer (if we have also virtual functions). So what happens when we delete &m? – Sydney.Ka Dec 29 '19 at 00:40
  • @Sydney.Ka I already told you. There is no meaning to your program, and thus no answer to that question. – Lightness Races in Orbit Dec 29 '19 at 00:46
  • Sorry I meant &b. I really want to understand what happens to the memory allocated . I already know about virtual constructors but I still don't know what happens to the memory allocated – Sydney.Ka Dec 29 '19 at 00:49
  • Repeating the same question over and over again won't change the answer. – Lightness Races in Orbit Dec 29 '19 at 00:51
  • I didn't repeat it – Sydney.Ka Dec 29 '19 at 00:52
  • You have asked three times what happens when you `delete &b`, and received the same response each time. The answer is _undefined_. _There is no answer_. I can't do anything about that, sorry. – Lightness Races in Orbit Dec 29 '19 at 00:52
  • Yes because I want to understand what happens in memory and not the behavior of the program. Sorry If I wasn't clear! – Sydney.Ka Dec 29 '19 at 00:56
  • 1
    What happens in memory is decided by the behaviour of the program. If there is no defined behaviour, it is impossible to predict what "happens in memory". I give up now - I recommend reading more about undefined behaviour and its consequences. It's not just a flag set on your source code; it's an actual thing that you need to seriously take into account. Good luck. – Lightness Races in Orbit Dec 29 '19 at 00:57