• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
xiajia

about "delete" and "delete []"

35 posts in this topic

compiling with VS2005,

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

line 4: delete p;

Edited by xiajia
0

Share this post


Link to post
Share on other sites

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):

[code] 

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;
}
[/code]

 

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

[quote]

 

$ 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:

 

[quote]

$ 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.

0

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
0

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

0

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.

0

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
0

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.
0

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).
0

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.

0

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0