943,651 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 756
  • C++ RSS
Sep 26th, 2008
0

priority of Function

Expand Post »
How can i change the priority of function?
I want to run nay other function before execution of main() function.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Sep 27th, 2008
0

Re: priority of Function

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:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. my_init_func();
  4.  
  5. ...
Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Sep 27th, 2008
0

Re: priority of Function

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Sep 27th, 2008
0

Re: priority of Function

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 27th, 2008
0

Re: priority of Function

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 ?
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 27th, 2008
1

Re: priority of Function

You might try something like ...
C++ Syntax (Toggle Plain Text)
  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()
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Sep 27th, 2008
0

Re: priority of Function

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
You might try something like ...
C++ Syntax (Toggle Plain Text)
  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!
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 27th, 2008
0

Re: priority of Function

However take into account the C++ Standard, 3.6.2 (3):
Quote ...
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):
Quote ...
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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to define a procedure adding new elemento to an array
Next Thread in C++ Forum Timeline: Adding Matrix-1 and Matrix-2 for Sum





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC