| View previous topic :: View next topic |
| Author |
Message |
Raj Guest
|
Posted: Wed Feb 22, 2006 8:06 am Post subject: Empty Main |
|
|
Could anyone tell me how to display "Hello World" in C++ without
writing anything inside main?i.e, main should not contain even a
single statement (no object creation or no cout or anything) |
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Wed Feb 22, 2006 8:06 am Post subject: Re: Empty Main |
|
|
"Raj" <raj.reshmi (AT) gmail (DOT) com> wrote in message
news:1140593268.885746.174340 (AT) f14g2000cwb (DOT) googlegroups.com...
: Could anyone tell me how to display "Hello World" in C++ without
: writing anything inside main?i.e, main should not contain even a
: single statement (no object creation or no cout or anything)
Have you studied the construction/destruction of global
objects yet ?
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form |
|
| Back to top |
|
 |
Harry Guest
|
Posted: Wed Feb 22, 2006 9:06 am Post subject: Re: Empty Main |
|
|
Try the following:
#include <iostream>
using namespace std;
class t
{
public:
t(){ std::cout<<"Hello world!"<<std::endl; }
};
t _t;
int main()
{
}
This should work. |
|
| Back to top |
|
 |
Fred Zwarts Guest
|
Posted: Wed Feb 22, 2006 1:06 pm Post subject: Re: Empty Main |
|
|
"Harry" <hapordigi (AT) gmail (DOT) com> wrote in message news:1140599038.819812.7910 (AT) g14g2000cwa (DOT) googlegroups.com...
| Quote: | Try the following:
#include <iostream
using namespace std;
class t
{
public:
t(){ std::cout<<"Hello world!"<<std::endl; }
};
t _t;
int main()
{
}
This should work.
|
If have seen that this works on some platforms.
On other platforms, however, it failed because cout is also a static object
and the constructor of _t was called when cout had not yet been constructed.
In such cases I use printf.
Fred.Zwarts. |
|
| Back to top |
|
 |
Raj Guest
|
Posted: Wed Feb 22, 2006 2:06 pm Post subject: Re: Empty Main |
|
|
Thanks a lot all of you..
I tried it and its working....
Thank you |
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Wed Feb 22, 2006 2:06 pm Post subject: Re: Empty Main |
|
|
"Fred Zwarts" <F.Zwarts (AT) KVI (DOT) nl> wrote in message
news:dthkko$p74$1 (AT) info (DOT) service.rug.nl...
:"Harry" <hapordigi (AT) gmail (DOT) com> wrote in message
news:1140599038.819812.7910 (AT) g14g2000cwa (DOT) googlegroups.com...
:> Try the following:
:>
:> #include <iostream>
:> using namespace std;
:>
:> class t
:> {
:> public:
:> t(){ std::cout<<"Hello world!"<<std::endl; }
:> };
:>
:> t _t;
:>
:> int main()
:> {
:>
:> }
:>
:> This should work.
:
:If have seen that this works on some platforms.
:On other platforms, however, it failed because cout is also a static
object
:and the constructor of _t was called when cout had not yet been
constructed.
:In such cases I use printf.
The C++ standard requires the above to work, and most of today's
implementations will be compliant with respect to this (there is
a common/published trick for the standard library to ensure the
timely initialization of std::cout).
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com |
|
| Back to top |
|
 |
Csaba Guest
|
Posted: Thu Feb 23, 2006 12:46 am Post subject: Re: Empty Main |
|
|
"Raj" <raj.reshmi (AT) gmail (DOT) com> wrote in news:1140593268.885746.174340
@f14g2000cwb.googlegroups.com:
| Quote: | Could anyone tell me how to display "Hello World" in C++ without
writing anything inside main?i.e, main should not contain even a
single statement (no object creation or no cout or anything)
|
---- begin hello.cxx ----
char *p = "Hellow, world";
---- end hello.cpp ----
Hey look, no main() at all !
Compile with one of:
g++ -E hello.cxx
g++ -S hello.cxx
( alas, g++ -c hello.cxx -o - doesn't seem to work )-:
Of course this prints some extraneous stuff, which can safely be ingnored
:-)
--
Life is complex, with real and imaginary parts. |
|
| Back to top |
|
 |
GB Guest
|
Posted: Thu Feb 23, 2006 7:06 am Post subject: Re: Empty Main |
|
|
Harry wrote:
| Quote: | Try the following:
#include <iostream
using namespace std;
class t
{
public:
t(){ std::cout<<"Hello world!"<<std::endl; }
};
t _t;
int main()
{
}
This should work.
|
A simpler approach that doesn't require defining a class is this:
#include <iostream>
std::ostream& s = std::cout << "Hello World\n";
int main()
{
} |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Feb 23, 2006 4:06 pm Post subject: Re: Empty Main |
|
|
Raj wrote:
| Quote: | Thanks a lot all of you..
I tried it and its working....
Thank you
|
Yeah, it's great when people do your homework for you isn't it. |
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Feb 25, 2006 9:06 pm Post subject: Re: Empty Main |
|
|
Harry wrote:
| Quote: | Try the following:
#include <iostream
using namespace std;
class t
{
public:
t(){ std::cout<<"Hello world!"<<std::endl; }
};
t _t;
int main()
{
}
This should work.
_t is an improper symbol name. Leading underscores in |
the global namespace are reserved to the implementation. |
|
| Back to top |
|
 |
Fred Zwarts Guest
|
Posted: Tue Mar 07, 2006 2:06 pm Post subject: Re: Empty Main |
|
|
Related to this topic, consider the following program:
class t
{
public:
t() { std::cout<<"Hello world!"<<std::endl; }
~t() { std::cout<<"Goodbye world!"<<std::endl; }
};
t T;
int main()
{
}
I understood from the discussion
that the C++ standard defines that the constructor of T is called after the construction of cout.
Does the C++ standard also tell that the destructor of T is called before the destruction of cout?
Regards,
Fred.Zwarts. |
|
| Back to top |
|
 |
|