Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Please let me know how to get this done. thanks
Posted
Comments
CPallini 17-Jun-11 3:15am    
It can be done as Cédric suggested, since global static variables are initialized before the main function is called. So if you initialize a static variable with a function call, such a function call will happen before main call.

As others pointed out, this doesn't make too much sense: why don't you just print your text at the beginning of your main method ?

Anyway, I guess there's a way to print something before the main: have a global static variable which is initialized by calling a function:

static int myVar = PrintString();

int PrintString() 
{
   cout << "My string";
   return 0;
}


But I don't see any reason why you would like to do something like that here... Furthermore, if you need this multiple times, there's no way to define the initialization order (if the static variables are in separate compilation units).
 
Share this answer
 
v2
Comments
CPallini 17-Jun-11 3:11am    
The question makes sense, of course, since the language allows it (just curiosity, in my opinion, makes sense). You provided the right solution, have my 5++.
Stefan_Lang 17-Jun-11 3:16am    
My 5, Good catch: I hadn't thought of initializing statics. I also agree with your concerns about initialization order, and that, therefore, it doesn't make sense to do this.
Cedric Moonen 17-Jun-11 3:26am    
In fact this "trick" comes handy in some specific situations: for a specific factory design pattern. If you make the factory a singleton, then each element that has to be created by the factory can register itself (before the main starts) using this technique. This allows to decouple the factory from the elements it has to create.
Sergey Alexandrovich Kryukov 17-Jun-11 22:16pm    
You're right, my 5. I failed to consider this possibility. I will remove my answer.
--SA
ShilpiP 18-Jun-11 2:49am    
Great ... have five... I am trying this solution with global variable but every time it shows cout as undeclared identifier.. :)
There's nothing you can do within the application to display a string before an application starts, however, you can use scripts to do something like that... you can create an application launch script that'll do whatever you need before starting the application.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 2:25am    
My 5, Albert. Well, your idea about script (batch file would do) is not without some sharpness but I seriously doubt it is applicable. I'm pretty sure it hardly could match the imaginary goal of OP which I fail to comprehend due to limitation of my own fantasy :-).
--SA
Albert Holguin 16-Jun-11 9:58am    
"imaginary goal"... lol... you need to read more science fiction.
CPallini 17-Jun-11 4:18am    
Nope: see Cédric solution.
Albert Holguin 17-Jun-11 8:53am    
interesting solution... hadn't thought of that either...
You can specify an alternate starting address via the Linker[^], but it is not really that different from starting at the normal point. Perhaps if you explained in more detail exactly what problem you are trying to solve someone may have a useful suggeston.
 
Share this answer
 
Let me understand this first. What you want to do is display some text before your main method is executed. You are kidding… right. You can not do so because your program must execute first and execution of a program starts from main so it's impossible do anything in your program if your program is not executing.
 
Share this answer
 
v2
Comments
Albert Holguin 16-Jun-11 2:02am    
my 5
Sergey Alexandrovich Kryukov 16-Jun-11 2:22am    
My 5.
(You might wonder why did I edit your answer. Well, mostly to demonstrate fixing "typographics". Pay attention for blank spaces after punctuation and HTML character entity instead of "....". Also, I changes one word for its non-jargon spelling as some non-native English speakers might misunderstand it. Hope you don't mind.)

--SA
CS2011 16-Jun-11 2:32am    
No Problem Mate.And thanks for your vote.
CPallini 17-Jun-11 3:18am    
That's wrong, since C++ allows that, see Cédric solution.
Just put the text to display, and its method of display, as the first item under MAIN.
Main will get called, this will show and then the rest of the program will run.
 
Share this answer
 
Comments
CPallini 17-Jun-11 4:17am    
You call this before main, don't you? :-)
That is an interview question I had. I found some thing like it in Java, but dont know how to code it in C++.

static
{
System.out.print("Hello ");
main(null);
System.out.println("Thank You! ");
System.exit(0);
}


Anyone has any idea? Thanks in advance.
 
Share this answer
 
Comments
Cedric Moonen 17-Jun-11 2:48am    
Look at the solution I provided (solution 7), this is doing something similar as your java code...
As pointed out above, your request cannot be solved, at least not when taken literally. The question is why would you want to do that? Or, more to the point, what is the actual requirement of the task?

I am asking, because far too often people get stuck with the idea to do something in a particular way, although it would be just one way of fulfilling a particular requirement. Way too often people then keep asking for help to realize a particular solution, rather than ask for help to fulfil a requirement.

You have to understand the difference between a solution and a requirement. What you have asked for sounds like its already part of a solution - but to help you we will need the orginal requirement, so we can suggest alternate solutions that you may not have thought of.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900