Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

psae0001

int main (void) INSIDE a class for C++ ???

This topic is 5221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I know we can put a main function inside a class like: public class myMain { public static void main (Strings args[]) { // whatever here } } But can we do that in C++??? I''m trying to see if it''s possible at least in DEV C++. -------- here is what I wrote and of course doesn''t work ---- // myMain.h #ifndef MYMAIN_H #define MYMAIN_H #include <stdio.h> #include <stdlib.h> #include <string.h> class MainConsole { public: MainConsole(); MainConsole(int); int showInt (); ~MainConsole(); private: int deInt; protected: int main (int , char*[]); }; #endif // End of MYMAIN_H // mymain.cpp #include "myMain.h" MainConsole::MainConsole (void) { deInt = 0; } MainConsole::MainConsole (int inputInt) { deInt = inputInt; } int MainConsole::showInt (void) { fprintf (stderr, "This program''s integer value is %d\n", deInt); return 100; } MainConsole::~MainConsole (void) { deInt = 0; } int MainConsole::main (int argc, char* argv[]) { if (argc < 2) { deInt = 18479; } deInt = showInt(); return 0; } Thank you for the help.

Share this post


Link to post
Share on other sites
Advertisement
Why would you need to do that? Seriously. It''s just not the way the language is designed. I suppose if you really wanted to put the body in a static function in a class then you could write a main function that simply delegates the work to that function, but why bother?

Share this post


Link to post
Share on other sites
No, I''m just curious IF it''s possible to do that in C++ as Java does?

But I guess what you are saying is the language (C++) was NOT designed or created to do so.

Did I get your message correct?

Share this post


Link to post
Share on other sites
The main function has to be outside any classes in order for the linker to find it.

Share this post


Link to post
Share on other sites
I think you''re confused.

#1) You can name your member functions anything you want as long as it''s a valid C++ identifier. This includes naming a member function main().

#2) The standard entry point of a C/C++ application is a global function called "main" with the following signature:

int main(int, char**)

#3) As the other poster stated, you can call your own main from the main function and this is what is typically done in a OO approach


[ CodeDread ]

Share this post


Link to post
Share on other sites
Ok, so from all the replies so far that C++ language was created DIFFERENTLY than Java.

In order to have a working Java program, the main function is suppose to be inside a class--THAT IS HOW THE DEVELOPER CREATED JAVA.

And C++ was created differently. In order to have a working C++ program--there must be an global function called "main" which was created by the C++ developer.

Therefore, what everyone saying is that it is ok to have my_main function that is called by the '' global function called "main" '', but is not necessary.

Am I correct?

Share this post


Link to post
Share on other sites
Yes that is correct. Technically, if you wrote your own runtime library you could get the first function to be called to be one in a predefined class, but that class would still have to have a specific name, you couldn''t do it as Java does. Although in normal use then the compiler expects there to a global function called main(or WinMain in the case of Win32 apps).

James

Share this post


Link to post
Share on other sites
quote:
Original post by jamessharpe
Technically, if you wrote your own runtime library you could get the first function to be called to be one in a predefined class, but that class would still have to have a specific name, you couldn''t do it as Java does. Although in normal use then the compiler expects there to a global function called main(or WinMain in the case of Win32 apps).


Well, main() isn''t always the first function called when you run your program. If you have global objects with contructors defined, then those ctors will be called before main() is executed. This can lead to some very obscure and confusing bugs, especially for a beginner who doesn''t expect anything in his code to be called prior to main().

Another reason not to use globals, btw.

Share this post


Link to post
Share on other sites
Agreed, but I think discussion of static initializors is outside the scope of the OP''s question

Yann when did you come back, btw?

Regards,
Jeff


[ CodeDread ]

Share this post


Link to post
Share on other sites
This is veering off topic but anyway

quote:
If you have global objects with contructors defined, then those ctors will be called before main() is executed.


I thought the ctors of all static objects (whether global or not) were called before main( ), or is it just when they first come into scope?

[edited by - Monder on June 4, 2004 6:41:04 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!