Can I invoke a function before main
I could do it by invoking it in a Global object's constructor .
Is there any other method other than this.
Manuel 6 4606
> Can I invoke a function before main I could do it by invoking it in a Global object's constructor . Is there any other method other than this.
Well, IMHO there is no other way to realize that in C++.
If you think on it for a little while you should be able to eliminate the
need with a simple redesign.
What's the difference between
foo() begin called before main() and
main()
{
foo();
// rest of code here
}
global objects are in general not a good idea,
keep them to simple data types like int,long,char,e tc.
The debug code on some machines (Microsoft Visual C++)
checks for memory leaks prior to destructing global objects
so objects like CString's will look like memory leaks
when they aren't.
"Manuel" <ma*******@hotm ail.com> wrote in message
news:b7******** *************** ***@posting.goo gle.com... Can I invoke a function before main I could do it by invoking it in a Global object's constructor . Is there any other method other than this.
Manuel
John Isaacks wrote: If you think on it for a little while you should be able to eliminate the need with a simple redesign.
What's the difference between foo() begin called before main() and
main() { foo(); // rest of code here }
global objects are in general not a good idea, keep them to simple data types like int,long,char,e tc.
The debug code on some machines (Microsoft Visual C++) checks for memory leaks prior to destructing global objects so objects like CString's will look like memory leaks when they aren't.
"Manuel" <ma*******@hotm ail.com> wrote in message news:b7******** *************** ***@posting.goo gle.com...
Can I invoke a function before main I could do it by invoking it in a Global object's constructor . Is there any other method other than this.
Manuel
1. Don't top-post, replies are either interspersed or
appended at the bottom.
There is much difference between a function being called
before main() and after. There is a lot that goes on
before the main() function is executed. For example,
constructors of global objects get called before the
main() function; but their order is unspecified.
The common method to have code executed before the
main() function is to consult your compiler or linker
documentation and see what function(s) get called
before main(). Many compilers for embedded systems
have "hooks" for functions to be called before
main(). If you have a function you want called
before main(), it may have to be written in assembly
depending on when the C and C++ initialization
takes place.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book ma*******@hotma il.com (Manuel) wrote in news:b753d529.0 307180418.3c976 985
@posting.google .com: Can I invoke a function before main I could do it by invoking it in a Global object's constructor . Is there any other method other than this.
Individual implementations may have extensions to do it. However, from a
strictly C++ standpoint, all you can do is rely on a global object's
constructor. However one thing to keep in mine is what the order of global
object constructors is.... within a single translation unit (generally
speaking, 1 .cpp file plus everything it includes) global objects are
constructed in the order that they are declared. The twist is that
_between_ translation units, it is undefined as to what order the
translation units are constructed in. So if you have two translation
units, with two global objects each (say A1, A2, B1, and B2), the order of
construction could be either A1 - A2 - B1 - B2, or B1 - B2 - A1 - A2. It's
up to the compiler to make that decision.
"Victor Bazarov" <v.********@att Abi.com> wrote in
news:vh******** ****@corp.super news.com: "Andre Kostur" <nn******@kostu r.net> wrote... ma*******@hotma il.com (Manuel) wrote in news:b753d529.0 307180418.3c976 985 @posting.google .com:
> Can I invoke a function before main > I could do it by invoking it in a Global object's constructor . > Is there any other method other than this.
Individual implementations may have extensions to do it. However, from a strictly C++ standpoint, all you can do is rely on a global object's constructor. However one thing to keep in mine is what the order of global object constructors is.... within a single translation unit (generally speaking, 1 .cpp file plus everything it includes) global objects are constructed in the order that they are declared. The twist is that _between_ translation units, it is undefined as to what order the translation units are constructed in. So if you have two translation units, with two global objects each (say A1, A2, B1, and B2), the order of construction could be either A1 - A2 - B1 - B2, or B1 - B2 - A1 - A2.
You say it like all objects are constructed in one unit and then all objects are constructed in the other unit. I cannot find anything to substantiate that, can you? Therefore, it seems that the orders A1-B1-B2-A2, B1-A1-B2-A2, etc., are just as possible.
True... I suppose theoretically their constructors could be interleaved.
About all you can rely on is that A1 will be constructed before A2 (and
likewise for B1 and B2).
All things have a beginning and that is the reason for main().
It's not good practice to have items as global because you can't
control the order in which the items are constructed.
If you remove your global items and have them created ( with the "new" )
from instead of main()
you can control the order of things. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Elven |
last post by:
Hi!
I've been trying to figure this out, but can't seem to find an answer.
Most likely it's some weird bug in my code, but since I don't have
access to the Standard, I wanted to cover my bases.. basically the
situation is that I'm writing a logging facility, class Log. Instances
of the class can be invoked to read a message by using the...
|
by: laredotornado |
last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to
have my Javascript function execute from the BODY's "onload" method,
but if there is already an onload method defined, I would like mine to
run immediately after it. So in the code below, what JS would i need
to add to my "myfile.inc" page so that I could guarantee this...
|
by: MuZZy |
last post by:
Hi,
I'm currently rewriting some functionality which was using
multithredaing for retrieving datasets from database and updating a grid
control. I found that the grids (Infragistics UltraGrid, though it
doesn't matter) were updated (i.e. grid.DataSource = dsDataSet;) and
used for different purposes in the worker thread, so now i'm wrapping...
|
by: Michael |
last post by:
Hi,
I'm having difficulty finding any previous discussion on this -- I
keep finding people either having problems calling os.exec(lepev), or
with using python's exec statement. Neither of which I mean here.
Just for a moment, let's just take one definition for one of the
|
by: Larax |
last post by:
Best explanation of my question will be an example, look below at this
simple function:
function SetEventHandler(element)
{
// some operations on element
element.onclick =
function(event)
{
| |
by: zaeminkr |
last post by:
I got a good answer here I have still confusing part.
I have two very simple classes
class DRect
{
private :
double x0, y0, x1, y1;
public :
DRect(double a, double b, double c, double d) : x0(a), y0(b),
|
by: aarklon |
last post by:
Hi all,
I have heard many discussions among my colleagues that main is a user
defined function or not.
arguments in favour:-
1) if it is built in function it must be defined in some header file
but it is n't.
|
by: Jon |
last post by:
Hi,
"THREAD_UI" = thread under which the UI controls were created.
"THREAD_XX" = another thread.
"FUNC_UI" = function that needs to run on THREAD_UI.
From THREAD_XX I need to start the execution of FUNC_UI, that needs to
run on THREAD_UI. Inside FUNC_UI, I will modify many controls present
on the form. Since I don't want to use...
|
by: AmberJain |
last post by:
HELLO,
Wikipedia defines return statement as:
Now there's a C program I have coded---->
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |