Class instances gone wrong!

Started by
25 comments, last by Servant of the Lord 10 years, 12 months ago

But in C++ you can shoot your foot off and reuse the bullet!

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement

This thread is a perfect example of why only experienced programmers should learn c++.

Not trying to start a holy war, c++ has its place, but the assumed knowledge to prevent shooting your own foot off is pretty high. smile.png

I was an experienced programmer when I started learning C++ years ago. I was never taught to use it properly as opposed to C. Either way, I still disagree.

Shogun.

A semi-serious issue with your code is that class objects aren't officially fully 'constructed' until you exit the constructor, so having your entire game loop within a constructor isn't a great idea - it might work, or might not. Chances are it'd probably usually work most of the time, but 'most of the time' isn't a good habit to get into with C++. smile.png
It's like playing Russian roulette - even if you have 1000 empty chambers, and only one chamber with a bullet in it, a smart programmer will tell you that that you shouldn't have any bullets in it, and a smarter programmer will tell you that it's better not to play Russian roulette at all.

Your main loop, in this case, should look like this:


int main( int argc, char* argv[] )
{
    game_app_t game; //No need to use dynamic allocation at all. 
    game.do_main_loop(); //Call 'glutMainLoop();' after your constructor has fully finished.
    return 0;
}

Yes, that's exactly what I'm doing now, except I didn't remove the dynamic allocation part though (will probably do that later), but now it looks like this:


int main( int argc, char* argv[] )
{
    game_app_t* app = new game_app_t();
    app->kick_off();
    
    return 0;
}

The pointer does get deallocated upon exiting because I used a function pointer passed to atexit() to ensure everything is released.

If that's also a bad idea, feel free to say so. I do appreciate everyone's advice. I'm learning something new from every response, and that's why I'm not afraid to make dumb threads! smile.png

Shogun

If that's also a bad idea, feel free to say so.

I don't know it's a bad idea, per se, but I do think it's a pointless one.

By the time your atexit() function gets called, you don't need to free memory (because the OS is just about to delete your entire address space), and you probably shouldn't rely on everything being intact to perform I/O or other complex shutdown tasks.

The best solution is not to use glut at all, and use any one of it's myriad modern replacements - none of which turn your main loop into a tail call.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Yes, that's exactly what I'm doing now, except I didn't remove the dynamic allocation part though (will probably do that later), but now it looks like this:


int main( int argc, char* argv[] )
{
    game_app_t* app = new game_app_t();
    app->kick_off();
    
    return 0;
}

The pointer does get deallocated upon exiting because I used a function pointer passed to atexit() to ensure everything is released.

If that's also a bad idea, feel free to say so. I do appreciate everyone's advice. I'm learning something new from every response, and that's why I'm not afraid to make dumb threads! smile.png

Shogun

Dude please, stop programming C++ stuff and pick a book or read some of the C++ faqs or the new approaches about C++11, but you are programming like if you were doing a thing between Java and C.

I learnt C++ years ago too, but one month ago when I backed to do C++ stuff I knew I had horrible bad practices and I had to solve them, so I just picked a quick summary comparing old C++ with C++11, together with the faqs are linked in this thread and I achieve to get a big knowledge about the language itself.

I know I still have some problems which usually they are design related, but at least I don't have serious problems with stack/heap memory or using macros (language stuff related).

While I concur with looking heavily into C++11 features, and with studying when to use dynamic memory and when not to, I don't think he should stop programming C++. Learning is half study and half practice.

I've wrote lots of dumb pieces of code - unless you're taught by another programmer (which I wasn't) who happens to be very skilled, you can't code perfectly right away, you learn better and better practices continually. Or, at least, eight years later I still am tweaking my practices when I discover better ways of doing things. When I first started out, my code was horrible. But you grow through usage.

@blueshogun96: Here's a practice tip, if you want one:
Write your program without using any 'new'/'delete' (or 'malloc'/'free', or similar functions). Don't use smart pointers either (for practice).
Declare everything locally, or as member-variables of a local class. This removes globals, and it removes dynamic memory. Things like vectors and maps are perfectly fine though.

Once you finish a project like that, then in future projects when you *need* to use dynamic memory, use smart pointers instead of new and delete.

This topic is closed to new replies.

Advertisement