You can't. The main() function is, by definition, the first function your program runs.
What difference does it make? Just call your function first thing in main:
int main()
{
my_init_func();
...
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
no man I am talking about execution. In above case first main will execute and then my_init_func() will be called.I dont want that.
See in following case
int main()
{
cout<<"In main"<
Duoas was talking about execution order too. I can't think of any possible way to run the above program and get the results you want. The program above won't even compile.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
You might try something like ...
#include <iostream>
using namespace std;
struct test
{
void myfunction()
{
cout << "myfunction()\n";
}
test()
{
myfunction();
}
};
static test testing;
int myfunction2()
{
cout << "myfunction2\n" << endl;
return 43;
}
static int test2 = myfunction2();
int main(void)
{
cout << "main()\n";
cin.get();
return 0;
}
I'd be interested to know why is this important to you?
[EDIT]
However, the safest way is simply to call your function first thing in the main(), i.e. not relying on the order of initialization.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
You might try something like ...
#include <iostream>
using namespace std;
struct test
{
void myfunction()
{
cout << "myfunction()\n";
}
test()
{
myfunction();
}
};
static test testing;
int myfunction2()
{
cout << "myfunction2\n" << endl;
return 43;
}
static int test2 = myfunction2();
int main(void)
{
cout << "main()\n";
cin.get();
return 0;
}
I'd be interested to know why is this important to you?
Wow. Cool. Learn something new every day!
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
However take into account the C++ Standard, 3.6.2 (3):
It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.[Example:
// - File 1 -
# include "a.h"
# include "b.h"
B b;
A::A() {
b.Use ();
}
// - File 2 -
# include "a.h"
A a;
// - File 3 -
# include "a.h"
# include "b.h"
extern A a;
extern B b;
int main () {
a.Use ();
b.Use ();
}
It is implementation-defined whether either a or b is initialized before main is entered or whether the initializations are delayed until a is first used in main. In particular, if a is initialized before main is entered, it is not guaranteed that b will be initialized before it is used by the initialization of a, that is, before A::A is called. If, however, a is initialized at some point after the first statement of main, b will be initialized prior to its use in A::A. —end example]
Some definitions from 3.6.2 (1):Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. A reference with static storage duration and an object of POD type with static storage duration can be initialized with a constant expression (5.19); this is called constant initialization. Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place.
I don't know compilers which perform deferred static objects initialization (and where this well-known trick does not work) but these compilers are standard-conformed...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348