| | |
priority of Function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
Hope this helps.
What difference does it make? Just call your function first thing in main:
C++ Syntax (Toggle Plain Text)
int main() { my_init_func(); ...
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
•
•
•
•
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"<<endl;
}
my_function()
{
cout<<"In my function";
}
O/P should be:
In my function
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.
•
•
Join Date: Nov 2007
Posts: 979
Reputation:
Solved Threads: 209
You might try something like ...
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.
C++ Syntax (Toggle Plain Text)
#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.
Last edited by mitrmkar; Sep 27th, 2008 at 3:33 am. Reason: myfunction2()
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
•
•
•
•
You might try something like ...
C++ Syntax (Toggle Plain Text)
#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?
However take into account the C++ Standard, 3.6.2 (3):
Some definitions from 3.6.2 (1):
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...
•
•
•
•
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]
•
•
•
•
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.
Last edited by ArkM; Sep 27th, 2008 at 12:20 pm.
![]() |
Similar Threads
- PopUp load priority (JavaScript / DHTML / AJAX)
- Issues with double linked list (C++)
- Help with priority queues (C++)
- Toshiba Satellite A20 Boot Priority Problem (Troubleshooting Dead Machines)
- linked list small problem with Insert Function (C++)
- priority queue (C++)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: How to define a procedure adding new elemento to an array
- Next Thread: Adding Matrix-1 and Matrix-2 for Sum
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






