Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Class instances gone wrong!

  • You cannot reply to this topic
26 replies to this topic

#21 Paradigm Shifter   Crossbones+   -  Reputation: 5412

Like
0Likes
Like

Posted 22 March 2013 - 06:22 PM

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

Sponsor:

#22 blueshogun96   Crossbones+   -  Reputation: 1053

Like
0Likes
Like

Posted 22 March 2013 - 07:13 PM

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.


Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


#23 Servant of the Lord   Crossbones+   -  Reputation: 20377

Like
1Likes
Like

Posted 22 March 2013 - 07:46 PM

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;
}

Edited by Servant of the Lord, 22 March 2013 - 07:53 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#24 blueshogun96   Crossbones+   -  Reputation: 1053

Like
0Likes
Like

Posted 23 March 2013 - 12:36 PM

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


Edited by blueshogun96, 23 March 2013 - 12:38 PM.

Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


#25 swiftcoder   Senior Moderators   -  Reputation: 10242

Like
0Likes
Like

Posted 23 March 2013 - 03:49 PM

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 - Software Engineer @Amazon - [swiftcoding]


#26 Puyover   Members   -  Reputation: 189

Like
0Likes
Like

Posted 21 April 2013 - 10:24 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).



#27 Servant of the Lord   Crossbones+   -  Reputation: 20377

Like
1Likes
Like

Posted 21 April 2013 - 03:31 PM

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.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]






PARTNERS