Is it acceptable to call a destructor?

Started by
39 comments, last by yckx 11 years, 6 months ago
I would argue Brother Bob's way is most efficient and clean, and has the most elegant and clean syntax. Then again, I'm used to reading C++.

Laerning how to use the standard library (std::remove_if(), std::vertex()) and the language features (lambdas, auto type deduction) is the boss fight that will unlock the next level.

Stephen M. Webb
Professional Free Software Developer

Advertisement

I would argue Brother Bob's way is most efficient and clean, and has the most elegant and clean syntax. Then again, I'm used to reading C++.

Laerning how to use the standard library (std::remove_if(), std::vertex()) and the language features (lambdas, auto type deduction) is the boss fight that will unlock the next level.


The problem is this is a beginner's forum, and when you start putting lambda functions, using std::bind, functors, or any other somewhat advanced techniques, it's easy to get discouraged. As you said, you're used to looking at C++ (and, probably used to looking at C++11).

Do you really expect a beginner, who is just learning what a for loop does, to understand a lambda function?

So, IMO, this forum should stick to fundamentals and being obvious about how to conquer problems, and this would exclude any C++11.

The boss fight is for the experienced players, not someone who just picked up the joystick for the first time.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

At the level you're working at, you do not call destructors.

Normally people do not call the destructors. Since C++ is clearly pretty new to you, take the advice and do what everyone else does and don't call the destructors directly. Because if you do, you'll be back here in a week asking people to debug the weird crashes in your program and that road is going to be really hard work because suddenly you're in a place where very few people will be able to help you because everyone else doesn't call the destructors because they don't want to be in that place.

There is a reason people learning to juggle start with bean bags and not running chainsaws. It's less exciting, that's for sure. But it's also less exciting.

[quote name='Bregma' timestamp='1350671577' post='4991860']
I would argue Brother Bob's way is most efficient and clean, and has the most elegant and clean syntax. Then again, I'm used to reading C++.

Laerning how to use the standard library (std::remove_if(), std::vertex()) and the language features (lambdas, auto type deduction) is the boss fight that will unlock the next level.


The problem is this is a beginner's forum, and when you start putting lambda functions, using std::bind, functors, or any other somewhat advanced techniques, it's easy to get discouraged. As you said, you're used to looking at C++ (and, probably used to looking at C++11).

Do you really expect a beginner, who is just learning what a for loop does, to understand a lambda function?

So, IMO, this forum should stick to fundamentals and being obvious about how to conquer problems, and this would exclude any C++11.

The boss fight is for the experienced players, not someone who just picked up the joystick for the first time.
[/quote]
Fundamental does not equate simple. Everything in every language, literally, is just syntactic sugar on top of machine code that adds a levels of abstraction. The more of this sugar you exclude, the more of other things you have to learn instead. I don't think manually erasing elements is any more obvious than using functions that the standard library already provides.

I have already responded to an earlier poster that I agree that my solution as a whole may be too much for a beginner (and frankly, I only posted it because alvaro asked for it). But I also argued that, once you have accepted the solution in general, the C++11 features I included actually improves the simplicity of it.

Ignoring the C++11 features because they add complexity is wrong in my opinion. You can ignore my solution as a whole, but it would be outright stupid to remove the C++11 from it because that's going to turn it into a mess (or an even greater mess, if you like).

So, IMO, this forum should stick to fundamentals and being obvious about how to conquer problems, and this would exclude any C++11.


I love the irony.

This sort of thing isn't even a problem in the languages we regularly recommend to beginners. Maybe we should focus on excluding C++ altogether?

If we're going to include C++, then include it properly.

Are you saying I put it a step further, lol?

I'm unfamiliar with the syntaxes some of you are giving me, or I don't really find them very clean/readable (no offense). So to me it looks like I'm doing less work or I'm lacking in some area of efficiency or functionality. I kind of think that way whenever I run into an unfamiliar syntax or code that looks intimidating to read through.

Compared to the methods that have been discussed in the thread compared to my posted way of doing it, which is the best in terms of efficiency and cleanliness? In C++ or something similar, please. Or even pseudocode!

You can make a decision on basic points: how much objects you want to use, what features you want to have and so on It's another great lesson what you have to learn on programming. But I suggest you to do it as good as possible, to use it in the next time ;)

ADDED

After reading this post by Brother Bob, I wanna add that you extremely have to learn basics of working with memory. It will strongly help you in the future. As middle point I want you to get some examples of SmartPointers(WeakPtr, StrongPtr, AutoPtr and so on).

PS when you will get a point of code from Brother Bob and tell how to use it in real application, then they will call you advantaged programmer ;)
C x 2 C x o C x C f nice C x C s C x C c
I'm not just learning about how to use for loops, lol. If you want to explain lambda functions to me, then I would gladly accept it.I've been meaning to anyway.
The reason I'm asking about this topic is that I'm making a game, and I want to make it as streamlined and advanced as I can with the tools given to me.
Most of the time when I ask questions here it has to do with conventions. Like, is it accepted by the general programming community to do this or that, etc. and this is one such question. But thank you all for the information once more.
You could make your own Delete() method, and that would set a boolean flag in the object (IsNull, for example) to true and delete any memory you may have allocated. Then, you could have a get() function (Or integrate this into the class with the boolean flag) to make sure you never tried to access members of the class that weren't there anymore.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !


You could make your own Delete() method, and that would set a boolean flag in the object (IsNull, for example) to true and delete any memory you may have allocated. Then, you could have a get() function (Or integrate this into the class with the boolean flag) to make sure you never tried to access members of the class that weren't there anymore.

That is entirely the wrong approach. If you delete an object and access it later, then the object shouldn't have been deleted in the first place because it was still in use. You have a lifetime management issue in that case and the proper way to handle that is to ensure that objects that are in use are never deleted, not to delete them arbitrarily and add a safety net just in case.
The way I handle it is a boolean that determines whether or not the object "exists". If it doesn't exist, I make its position far from anything that it could interact with, I disable its actions, and erase it from the board. Then once your character is out of the environment that has the enemy objects, they stay in a state of nonexistence until the next time they need to be initialized. So I would delete them right before I make more of them, and if the program ends, I delete them regardless.

This topic is closed to new replies.

Advertisement