c++ Instantiating a class in another class

Started by
17 comments, last by SmkViper 9 years, 1 month ago
Upon exiting the program, a call will also be made to SDL_EnableUNICODE(SDL_DISABLE)[/quote] You don't need to do that.
Advertisement

@Wooh,

It may not be necessary but it's probably good practice, just like putting return 0, at the end of main.

Mike

@Wooh,

It may not be necessary but it's probably good practice, just like putting return 0, at the end of main.

Mike

One is explicitly returning the exit status instead of relying on an implicitly generated default, the other is changing global state after it has any meaning or use and right before it gets destroyed anyway.

So I would rather compare it to setting a member variable to 0 in your destructor.

f@dzhttp://festini.device-zero.de

It may not be necessary but it's probably good practice, just like putting return 0, at the end of main.

When using SDL it is not only a matter of good practice to explicitly return from main but something you should always do.

On some platforms (e.g. Windows) SDL uses its own main function and #defines main as SDL_main in the header.
#define main    SDL_main

This means that the main function you define is not really the main function at all, but a function named SDL_main. Leaving out the return statement from a value-returning function other than main results in undefined behavior, so it's something you want to avoid.

It may not be necessary but it's probably good practice, just like putting return 0, at the end of main.

When using SDL it is not only a matter of good practice to explicitly return from main but something you should always do.

On some platforms (e.g. Windows) SDL uses its own main function and #defines main as SDL_main in the header.

#define main    SDL_main
This means that the main function you define is not really the main function at all, but a function named SDL_main. Leaving out the return statement from a value-returning function other than main results in undefined behavior, so it's something you want to avoid.


I would say #define-ing language constructs is a WTF all its own... O_o

There's nothing quite like lying to the programmer about what is going on.
Tell that to the SDL devs. :/ I have no idea why they can't do whatever they are doing in SDL_Init() and SDL_Quit() instead.

It may not be necessary but it's probably good practice, just like putting return 0, at the end of main.

I think you are mixing up two different ideas.

It's good practice to clean up memory and other resources when you are done using them. This helps prevent things from being leaked.

It's good practice to set pointers to null after freeing the pointer, unless you know the pointer itself is about to be erased. This helps prevent accidental double deletion, dangling pointers, and memory leaks.

It's a pointless (but not harmful) practice to set your flags after they are no longer in use.


bool meow = true;
 
//...code that uses 'meow'...
 
meow = false; //Pointless. It's just a boolean, and since it is no longer used after this point, it could be set to 'aardvark' for all the code cares.

SDL_EnableUNICODE(SDL_ENABLE);
 
//...code that uses the hidden internal boolean...
 
SDL_EnableUNICODE(SDL_DISABLE); //Pointless. It's just (the equivalent of) a boolean.

There are things that need to be cleaned up or de-initialized, but I'm pretty positive this isn't one of them.


Tell that to the SDL devs. :/ I have no idea why they can't do whatever they are doing in SDL_Init() and SDL_Quit() instead.
Cross-platform convenience of the end-user. With Win32, the entry point for applications is WinMain() not main(), unless you are running a console application or explicitly set certain Visual Studio flags.

#define main SDL_main is a hack, yes, but a mostly harmless and user-friendly one. Sometimes the cleanest way to do something is unfortunately a hackish way; in those situations, the smaller and more self-contained the hack, the better.


#define main SDL_main is a hack, yes, but a mostly harmless and user-friendly one. Sometimes the cleanest way to do something is unfortunately a hackish way; in those situations, the smaller and more self-contained the hack, the better.

I once had to use a library with a #define main hack along with another library that had a variable named main in a header file.

Tell that to the SDL devs. :/ I have no idea why they can't do whatever they are doing in SDL_Init() and SDL_Quit() instead.



Cross-platform convenience of the end-user. With Win32, the entry point for applications is WinMain() not main(), unless you are running a console application or explicitly set certain Visual Studio flags.

#define main SDL_main is a hack, yes, but a mostly harmless and user-friendly one. Sometimes the cleanest way to do something is unfortunately a hackish way; in those situations, the smaller and more self-contained the hack, the better.


Sometimes, sure. But... this seems like taking cross-platform to the extreme. If you're really that concerned with hiding the platform-dependent entry point from the library user, why not just tell them to make a SDL_main() with SDL's own rules and parameters? Why tell your users to use C's main function when it sometimes isn't C's main function? Especially when C's main function has special properties that SDL can't replicate without modifying the compiler...

That's what I'd expect most object-oriented APIs to do - give me some kind of "Application" object that I override a "Run" function on.

Edit: Bah - apologies - this is going off track of the original thread.

This topic is closed to new replies.

Advertisement