Вопрос

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 )

Это было полезно?

Решение 2

@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!

Другие советы

It is a Borland specific directive, still documented as well. It just lets you specify a function that runs before the main() function is entered. You normally use it for an initialization function. It is a small band-aid for the infamous "static initialization order fiasco" that C and C++ implementations suffer from. Not so sure it stops the bleeding, having to pick the priority number ought to get impractical when the program gets large.

Anyhoo, nothing you ought to use as a replacement of main(). Although I'd suspect you could make it work as long as you don't return from the function and call exit(). You can't use an argument of course, there's nothing meaningful that the CRT can pass. So overloads can't work either. Stick with main().

I can neither find any documentation on your compiler, nor use the compiler itself to check my guess. But from what I remember, #pragma startup takes function without arguments and returning void.

I would further guess that your code is not going to work if you leave only function taking int argument.

Disregarding that, I think it's not possible to pass an argument to function called in such a way.

As a solution I would propose you creating wrapper function, calling whatever function you would like with necessary arguments, like:

void start()
{
#pragma startup start
    fun1( global_var_name );
}

I'm not exactly sure I understand the restrictions, but how about simply defining another function with a different name to forward to the function you want?

void fun2() { fun1(42); }

#pragma startup fun2 100
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top