priority of Function

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

priority of Function

 
0
  #1
Sep 26th, 2008
How can i change the priority of function?
I want to run nay other function before execution of main() function.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: priority of Function

 
0
  #2
Sep 27th, 2008
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:
  1. int main()
  2. {
  3. my_init_func();
  4.  
  5. ...
Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

Re: priority of Function

 
0
  #3
Sep 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: priority of Function

 
0
  #4
Sep 27th, 2008
Originally Posted by san_sarangkar View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: priority of Function

 
0
  #5
Sep 27th, 2008
main() is the point of entry to your program .. so there is no way around it. I am not even sure why you want to do this ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 980
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: priority of Function

 
1
  #6
Sep 27th, 2008
You might try something like ...
  1. #include <iostream>
  2. using namespace std;
  3. struct test
  4. {
  5. void myfunction()
  6. {
  7. cout << "myfunction()\n";
  8. }
  9.  
  10. test()
  11. {
  12. myfunction();
  13. }
  14. };
  15.  
  16. static test testing;
  17.  
  18. int myfunction2()
  19. {
  20. cout << "myfunction2\n" << endl;
  21. return 43;
  22. }
  23.  
  24. static int test2 = myfunction2();
  25.  
  26. int main(void)
  27. {
  28. cout << "main()\n";
  29.  
  30. cin.get();
  31.  
  32. return 0;
  33. }

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()
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: priority of Function

 
0
  #7
Sep 27th, 2008
Originally Posted by mitrmkar View Post
You might try something like ...
  1. #include <iostream>
  2. using namespace std;
  3. struct test
  4. {
  5. void myfunction()
  6. {
  7. cout << "myfunction()\n";
  8. }
  9.  
  10. test()
  11. {
  12. myfunction();
  13. }
  14. };
  15.  
  16. static test testing;
  17.  
  18. int myfunction2()
  19. {
  20. cout << "myfunction2\n" << endl;
  21. return 43;
  22. }
  23.  
  24. static int test2 = myfunction2();
  25.  
  26. int main(void)
  27. {
  28. cout << "main()\n";
  29.  
  30. cin.get();
  31.  
  32. return 0;
  33. }

I'd be interested to know why is this important to you?
Wow. Cool. Learn something new every day!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: priority of Function

 
0
  #8
Sep 27th, 2008
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...
Last edited by ArkM; Sep 27th, 2008 at 12:20 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 665 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC