Search code examples
c++startuppragma

How to use #pragma startup with overloaded functions?


Suppose I have two overloaded functions :

fun1(int)
fun1()

In this case how can I use #pragma startup directive to start my program's execution with fun1(int) ?

The syntax only contains the function name :

#pragma startup fun1 100

Is there any way by which I can make a choice between these two functions?

UPDATE:

compiler- turbo c/c++ 3.1 (sorry for an old compiler )


Solution

  • @joey-rohan, I dont have borland but have tried to provide some code below to demonstrate:

    #pragma start requires a non parameterised function, the number after the function call is the priority of the function where 0 is the highest and 255 the lowest. The function should ideally (varies in some compilers) be defined prior to the #pragma call. Source: embarcadero

    I have done a bit of scouting about and think that the solution to your dilemma would be to use a mixture of #define and #pragma to achieve what you would like to do. For example something like:

    #include <iostream>
    
    #define myvalue 100
    #define usemyvalue 0
    
    void fun1(int passedInValue)
    {
        // carry out function here
        std::cout << "I was passed the value:" << passedInValue << std::endl;
    }
    void fun1(void)
    {
        std::cout << "in fun1(void)\n";
        std::cout << "Use my value = " << usemyvalue<< std::endl;
        if (usemyvalue==1)
        {
            std::cout << "Using fun1(int) with a value!\n";
            fun1((int)myvalue);  // remember to cast as an int here
        }
        else
        {
            //normal fun1()code here
            std::cout << "No var passed!\n";
            std::cout << "Running standard non parametrised code!\n";
        }
    
    }
    
    #pragma start fun1 10
    
    
    int main()
    {
       std::cout << "Hello World\n";
    
       return 0;
    }
    

    I know this is not as elegant as I would wish, therefore its probably not as elegant as you would like either, however it does allow for the functionality you need, with minimal modification. Unfortunately i only have GCC available to test against on this machine and it does not seem to support #pragma start it does however support a differing way of achieving the same (as shown on C Language Constructors and Destructors with GCC so here is some code for GCC which I could test to let you see how to achieve what you are asking (because I would hate to pst a methodology that cannot be proven):

    #include <iostream>
    
    #define myvalue 100
    #define usemyvalue 1   // this is the control switch to determine which to use, 
                           // if the value is 1 then will pass a variable, otherwise will use 
                           // the fun1(void) function
    void fun1 (void) __attribute__((constructor));
    
    
    void fun1(int passedInValue)
    {
        // carry out function here
        std::cout << "I was passed the value:" << passedInValue << std::endl;
    }
    void fun1(void)
    {
        std::cout << "in fun1(void)\n";
        std::cout << "Use my value = " << usemyvalue<< std::endl;
        if (usemyvalue==1)
        {
            std::cout << "Using fun1(int) with a value!\n";
            fun1((int)myvalue);  // remember to cast as an int here
        }
        else
        {
            //normal fun1()code here
            std::cout << "No var passed!\n";
            std::cout << "Running standard non parametrised code!\n";
        }
    
    }
    
    #pragma startup fun1
    
    int main()
    {
        std::cout << "now running main function.\n";
        std::cout << "Hello World\n"; 
        return 0;
    }
    

    I suspect that the second method would also work with Borlands compiler but without access to it I cannot swear to that.

    Hope this helps!