• Advertisement
Sign in to follow this  

about "delete" and "delete []"

This topic is 1824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

compiling with VS2005,

error C2248:'tempClass<T>::~tempClass' : cannot access private member declared in class 'tempClass<T>'

line 4: delete p;

Edited by xiajia

Share this post


Link to post
Share on other sites
Advertisement

Do we have to guess the error? Or the intended behaviour?

 

Presumably from your "friend" declaration, you want "tempClass" to only be created and destroyed by "tempClassA".

 

You could clarify this using better naming (these two class names are confusingly similar):

 

template<typename T>
void XDELETE(T *&p)
{
    delete p;
    p = nullptr;
}
 
template<class T> class Example;
 
template<class T> class MostlyPrivate
{
private:
    friend class Example<T>;
 
    T * m_p;
    
    MostlyPrivate()
        :m_p(nullptr)
    {
    }
    
    ~MostlyPrivate()
    {
        XDELETE(m_p);
    }
};
 
template<class T> class Example
{
public:
    Example()
        :m_a(new MostlyPrivate<T>())
    {
    }
    
    ~Example()
    {
         XDELETE(m_a);
    }
    
private:
    MostlyPrivate<T> * m_a;
};
 
int main()
{
    Example<int> a;
    return 0;
}

 

The compiler error (having corrected some of the more trivial errors) says:

 

$ g++ test.cpp -std=c++0x
test.cpp: In function ‘void XDELETE(T*&) [with T = MostlyPrivate<int>]’:
test.cpp:38:10:   instantiated from ‘Example<T>::~Example() [with T = int]’
test.cpp:47:18:   instantiated from here
test.cpp:23:5: error: ‘MostlyPrivate<T>::~MostlyPrivate() [with T = int]’ is private
test.cpp:4:5: error: within this context

[/quote]

 

Highlighting the most relevant lines:

 

$ g++ test.cpp -std=c++0x
test.cpp: In function ‘void XDELETE(T*&) [with T = MostlyPrivate<int>]’:
test.cpp:38:10:   instantiated from ‘Example<T>::~Example() [with T = int]’
test.cpp:47:18:   instantiated from here
test.cpp:23:5: error: ‘MostlyPrivate<T>::~MostlyPrivate() [with T = int]’ is private
test.cpp:4:5: error: within this context

[/quote]

Essentially, the problem is that XDELETE is trying to call a private destructor. Even though "Example" is a friend, and the call to XDELETE is coming from "Example", that does not confer friendship onto XDELETE. Note this is an interesting case of where the macro version could be considered to be "bypassing" language rules.

 

One solution is to add XDELETE as a friend too. Another is to consider making the destructor public - perhaps making the constructor private is enough to prevent whatever mishaps you're worried about?

 

The most obvious solution is to use delete, not XDELETE, because you are in a destructor and thus it would be undefined behaviour for any code to touch this pointer again anyway.

Share this post


Link to post
Share on other sites
Actually, XDELETE is an abomination. In correctly written code, setting the pointer to 0 will never matter, so it's a wasted operation. In poorly written code, it will sometimes help (if someone tries to access a member through the pointer after it has been deleted) and sometimes hurt (if someone tries to delete the pointer again, which is a problem that will now be masked).

You are also missing a semicolon after the tempClassA block, and the word `class' after `friend'.

Why do you feel the need to call `new' at all? I rarely do it. Your members should be objects, not pointer to objects, unless you have a good reason ("I used to be a Java programmer and feel the need to write `new' all over the place" is not a good reason). Edited by Álvaro

Share this post


Link to post
Share on other sites

I think "make the destructor public" is better than "add XDELETE as a friend to class MostlyPrivate",biggrin.png

Share this post


Link to post
Share on other sites

about the semicolon after the tempClassA,it is a small accident.happy.png the main function is added later.The compiler will not complain if not coupled.

Share this post


Link to post
Share on other sites
Why do you feel the need to call `new' at all? I rarely do it. Your members should be objects, not pointer to objects, unless you have a good reason ("I used to be a Java programmer and feel the need to write `new' all over the place" is not a good reason).

do you mean call ‘new’ at the constructor?This is just one case.Not always the case.

Edited by xiajia

Share this post


Link to post
Share on other sites

Why do you feel the need to call `new' at all? I rarely do it. Your members should be objects, not pointer to objects, unless you have a good reason ("I used to be a Java programmer and feel the need to write `new' all over the place" is not a good reason).

do you mean call ‘new’ at the constructor?This is just one case.Not always the case.



No, I mean what I said. I very rarely use `new' in my code. When I do, it's almost always in factory classes.

Share this post


Link to post
Share on other sites
I'm not sure why you're objecting in this case. Heap allocating the reference count in a non-intrusive reference counting smart pointer is perfectly normal. Not that there aren't other issues to look at here (such as those already mentioned as well as exception safety).

Share this post


Link to post
Share on other sites

I have yet to learn the knowledge of design patterns, I learned the factory pattern to do some more in-depth discussion.

Share this post


Link to post
Share on other sites

I guess the objection is simply because with only the latest example code, it boils down to

class MyClass
{
public:
    MyClass() : ptr_(new something) {}
    ~MyClass() { delete ptr_; }
};

 

That on its own is usually not just pointless ("something" should just be a regular member), but also a time bomb just waiting for an instance being accidentally copied (assigning it or passing it to a standard container). So usually whenever you see this kind of pattern and it really is necessary, explicitly making it non-copyable or supplying assignment and copy constructor should become almost a reflex.

 

Since this is just example code, it most likely doesn't apply, but still, I've encountered production code where the author thought that setting the pointer to NULL in the destructor will somehow magically affect all the other objects that have a copy of that pointer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement