views:

171

answers:

4

hello community, how are you?

in the following code:

class x
{
private:
 someRef& m_ref;

public:
 x(someRef& someRef):m_ref(someRef)
{ 
}

do I need to do:

~x()
{
  delete m_ref;
}

which by the way doesnt work without getting the pointer...

basically I'm asking: Do I need to call a destructor on a reference member?

+10  A: 

No.

You only need to delete an object if you own it. If you were passed a reference, it means that someone else owns it, thus it's unnecessary and thankfully the language prevents it.

Matthieu M.
How does the language prevent it?
Troubadour
@Troubadour `foo delete a;` will fail (except when foo is a pointer type).
tstenner
@Troubadour: as the OP mentionned, it simply does not compile.
Matthieu M.
@tstenner, @Matthieu M.: That's not the same as the "language prevents it". Just do `foo
Troubadour
@Troubadour: ah but here you're deleting a pointer. The way you obtained the pointer doesn't matter to the language (that's C++), but you had to do extrawork to get it, and hopefully you realized that you were doing it wrong.
Matthieu M.
@Matthieu M.: Do you agree that the language doesn't prevent it?
Troubadour
@Troubadour: we are not talking about the same thing :) The language prevents the deletion of a random reference, but let you call `delete` on whatever pointer. So I disagree with you on that point, but agree with you on the general point that the language does not prevent much.
Matthieu M.
@Matthieu M.: To a beginner the second sentence in your answer implies two things. Firstly that passing a reference guarantees that the ownership lies elsewhere and secondly that the language prevents you from deleting an object if all you have is a reference to it. Both of these are not true. I know exactly what you mean, I just don't think it's clear enough to a beginner.
Troubadour
@Troubadour: Ah! Sorry, I was not definitely not talking with a beginner mindset. I am afraid C++ is very unkind toward beginners... there are just too many pitfalls when it comes to object lifetime that the other languages don't expose any longer.
Matthieu M.
+1  A: 

No. You can only delete pointers, not references, and even then you must only delete objects that you allocated using the new operator. And then you must be sure to delete them only once. Here is the case in which you would need to use delete in your destructor:

class x
{
private:
 someObj* m_ptr;

public:
 x():m_ptr(new someObj())
{ 
}

 ~x()
{
  delete m_ptr;
}

But in general it's best to avoid even this and use smart pointers instead.

Tyler McHenry
+3  A: 

I don't think one actually strictly speaking ever deletes even pointers. What you delete are dynamically allocated objects (or arrays of objects) that the pointer is a handle for. If the object originates from a call to new and it is the responsibility of this class to clean up after this object, then you call delete.

It is technically possible that a reference might be referring to a dynamically allocated object:

int main()
{
    //in principle a reference can also refer to a dynamically allocated object
    x var(*new someRef); 
}

//and if that is the intended usage:
x::~x()
{
  delete &m_ref;
}

However, this would be incredibly bad style. By convention, the "owning" handle of a dynamically allocated object should not be a reference.

UncleBens
+1  A: 

I want to clarify some misconceptions you seem to have that are beyond the intent of your question:

When a class's destructor is called all of it's members' destructors get called as well.

Calling delete is not the same as calling the destructor. delete explicitly calls the destructor and also calls operator delete at the objects location, it is a 2 part thing.

Dave