Check this out !

This is a discussion on Check this out ! within the C++ Programming forums, part of the General Programming Boards category; Some of you probably know how to do this, but i just discovered how to encapsulate the main function. Using ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    108

    Lightbulb Check this out !

    Some of you probably know how to do this, but i just discovered how to encapsulate the main function. Using Dev C++ 4.9.9.0 i got this bit of code to compile and link with 1 warning issued: resolving _WinMain@16 by linking to _WinMain

    Code:
    #include <iostream>
    
    class main
    {
    public:
        main()
        {
            std::cout << "Hello !";
            exit(0);
        }    
    };
    
    main WinMain;
    It works perfectly, you are just using a WinMain "function" object that does not require 16 bytes of data to be passed to it.
    Last edited by DarkStar; 08-09-2004 at 08:49 PM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    looks like main( ) is playing the roll of a constructor in this situation.

    some people don't like exit( ) but i think it's cool
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    My guess is that what you see is the constructor at work while the linker, unable to find his entry function, linked to some default function that is empty.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    This might throw up some bugs too. Compilers often add code before and after the entry point (this is why you need to call exit() - the compiler usually provides that and passes the return of main to it). It's possible that some initialisation code could be missed and this could possible cause some problems with various libraries you are using or even maybe lead to a crash. Who knows - it's not standard so anything's feasible

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Yes, I see it more as some sort of a bug in ld.exe that it allows it to go thru with just a warning. You should not really be able to do this, it's sort of like the public static main in a Java app.

    Here is a better way of writing this...

    Code:
    #include <iostream>
    
    class Application
    {
        char *s;
    public:
        Application(): s("Hello !")
        {
            std::cout << s;
            std::getchar();
            std::exit(0);
        }
    }
    WinMain;

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Quote Originally Posted by nvoigt
    My guess is that what you see is the constructor at work while the linker, unable to find his entry function, linked to some default function that is empty.
    Your right nvoigt, that's what is really happening.
    Last edited by DarkStar; 08-10-2004 at 11:10 AM.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    I have found a way to get this to link without a warning.

    Code:
    #include <iostream>
    
    class winapp
    {
    public:
        winapp()
        {
            std::cout<<"winapp()\n";
            std::exit(0);
        }
    }
    WinMain;
    g++ winapp.cpp -owinapp -enable-stdcall-fixup

    Very unusual that you are able to do this.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I'm actually surprized that exit() is more common in C than in C++ and that most people don't like it. With atexit() and all it seems like something that would've been more popular among the OOP bunch...

  9. #9
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    I got this to work, its a pretty cool trick, but does it have any advantages? The only thing I can think of is being able to organize variables easier(in the class instead of in main)

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    No it's non standard, non portable and could lead to unforseen problems

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Fordy is right. It's just something interesting and unusual and nothing more than that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21