Thread: Ummm... Help me be annal retentive?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Ummm... Help me be annal retentive?

    Ok, so I want to do a simple thing, but it looks like the compiler doesn't like me doing it. Here's some example code (posting my real code would fill this up with too much clutter)

    Code:
    int SomeFunctionOrWithMoreTypingAMethodInAnObject()
    {
        // do other stuff
        return 0;
    }
    
    int main (...)
    {
        // do stuff
        return 0;
    }
    
    /* This stuff is presumably run before we enter main,
     * during the assignment and memory creation stage
     * (I think there's a better word for that thing I can't
     * seem to remember the name of 0.o)
     */
    
        // This ~works~ but then I'll probably have a bunch of nop's everywhere
        int nop = SomeFunctionOrWithMoreTypingAMethodInAnObject();
    
        // This give an error (on gcc 4.2.2 at least)
        {
            int nop = SomeFunctionOrWithMoreTypingAMethodInAnObject();
        }
    
        // This also give an error (on gcc 4.2.2 at least)
        SomeFunctionOrWithMoreTypingAMethodInAnObject();
    In my case I'm trying to assign values to a settings class by calling a function. To assign the values I need only 2 parameters, so I suppose I could use the operator[]. I was hoping to at least understand why anything other then assignment (including scoping variable) can't be done before getting to main. Or better yet, how to get a few lines to execute before entering main, without some weird "hack" like the nop assignment.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nothing should execute before main (except compiler specific stuff). In main, you do your initialization and your app stuff.
    Unless I'm off the hook here?

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Global object constructors run before main().

    gg

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Global object constructors run before main().
    In other words, create a class that calls your function(s) in its constructor, then make a global instance of that class.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Where exactly is this bit of code that you've shown after the multi line comment?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps we should be asking why the code should run before main because that's not how a typical application should work. Do your code in main, that's the usual line. So why are you trying to run code before main?

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    @Elysia
    I want to do something like this:

    Code:
    CSettings::GetInstance()->SetDefault("WindowTitle", PROGRAM_NAME);
    I'd like to do it in screen.cpp and input.cpp, etc... so that the application defaults are set before main. Then I'd like main to start, and eventually request a setting value:

    Code:
    int main(...) {
        // ...
        int iWidth = CSettings::GetInstance()->GetValue<int>("ScreenWidth");
        // ...
    }
    When GetValue is called for the first time, it will load the users setting file, and possibly replace any application default settings. It'll be up to me to keep any calls to GetValue inside main, but I can deal with that.

    I could do this in main (a few ways i suppose) but all my default settings would have to be in one place. I'd rather have screen specific settings in the screen.cpp file, and I'd rather not have to remeber to call ScreenLoadDefaultSettings, etc... for each file.


    @Codeplug?/Daved
    I like that idea, I suppose I could put a "static CDefaultSettings DefaultSettings();" in each cpp, and make a virtual class in the settings header to help. I'd end up using some extra memory though.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by deadram View Post
    @Elysia
    I want to do something like this:

    Code:
    CSettings::GetInstance()->SetDefault("WindowTitle", PROGRAM_NAME);
    I'd like to do it in screen.cpp and input.cpp, etc... so that the application defaults are set before main. Then I'd like main to start, and eventually request a setting value:
    Now here's another question:
    What exactly does it do that it has to be done in every source file? It looks like a function to set the default title on a window or something.

    Quote Originally Posted by deadram View Post
    Code:
    int main(...) {
        // ...
        int iWidth = CSettings::GetInstance()->GetValue<int>("ScreenWidth");
        // ...
    }
    When GetValue is called for the first time, it will load the users setting file, and possibly replace any application default settings. It'll be up to me to keep any calls to GetValue inside main, but I can deal with that.
    What I don't get is why you can't do the init inside main. You do init inside every file and main has to what, exactly? Why can't you just do the init in main first before fetching the value?

    Quote Originally Posted by deadram View Post
    I could do this in main (a few ways i suppose) but all my default settings would have to be in one place. I'd rather have screen specific settings in the screen.cpp file, and I'd rather not have to remeber to call ScreenLoadDefaultSettings, etc... for each file.
    So make an init function in screen.cpp to handle all that then.

    Quote Originally Posted by deadram View Post
    @Codeplug?/Daved
    I like that idea, I suppose I could put a "static CDefaultSettings DefaultSettings();" in each cpp, and make a virtual class in the settings header to help. I'd end up using some extra memory though.
    This is what I rather call poor programming for a little laziness.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It looks like you just need to put your initialisation code in the CSettings constructor.
    Beware of the static data initialization order problem though: http://www.parashift.com/c++-faq-lit...html#faq-10.12
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ummm
    By Fountain in forum Tech Board
    Replies: 15
    Last Post: 11-13-2002, 05:36 AM
  2. Replies: 3
    Last Post: 10-01-2001, 12:17 PM