C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Empty Main

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Raj
Guest





PostPosted: Wed Feb 22, 2006 8:06 am    Post subject: Empty Main Reply with 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)
Back to top
Ivan Vecerina
Guest





PostPosted: Wed Feb 22, 2006 8:06 am    Post subject: Re: Empty Main Reply with quote



"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





PostPosted: Wed Feb 22, 2006 9:06 am    Post subject: Re: Empty Main Reply with 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.
Back to top
Fred Zwarts
Guest





PostPosted: Wed Feb 22, 2006 1:06 pm    Post subject: Re: Empty Main Reply with quote

"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





PostPosted: Wed Feb 22, 2006 2:06 pm    Post subject: Re: Empty Main Reply with quote

Thanks a lot all of you..
I tried it and its working....
Thank you
Back to top
Ivan Vecerina
Guest





PostPosted: Wed Feb 22, 2006 2:06 pm    Post subject: Re: Empty Main Reply with quote

"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





PostPosted: Thu Feb 23, 2006 12:46 am    Post subject: Re: Empty Main Reply with quote

"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





PostPosted: Thu Feb 23, 2006 7:06 am    Post subject: Re: Empty Main Reply with quote

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






PostPosted: Thu Feb 23, 2006 4:06 pm    Post subject: Re: Empty Main Reply with quote

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





PostPosted: Sat Feb 25, 2006 9:06 pm    Post subject: Re: Empty Main Reply with quote

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





PostPosted: Tue Mar 07, 2006 2:06 pm    Post subject: Re: Empty Main Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.