By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

A question in TC++PL!

P: n/a
Hi,

In TC++PL, Chapter 10, there is an exercise which ask us to modify the
following program:

#include <iostream>

int main()
{
std::cout << "Hello, world!/n" ;
return 0;
}

so that the output would be:

Initialize
Hello, world!
Clean up

but we should not change main().

I believe we will have to modify the constructor and desctructor of class
"ostream" so that the constructor of "ostream" will output
Initialize
and the destructor of "ostream" will output
Clean up

But I don't know if my idea is correct. Is there better idea? Thanks!
Aug 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Guofu Chen" <gc***@mathworks.comwrote in message
news:f9**********@fred.mathworks.com...
Hi,

In TC++PL, Chapter 10, there is an exercise which ask us to modify the
following program:

#include <iostream>

int main()
{
std::cout << "Hello, world!/n" ;
return 0;
}

so that the output would be:

Initialize
Hello, world!
Clean up

but we should not change main().

I believe we will have to modify the constructor and desctructor of class
"ostream" so that the constructor of "ostream" will output
Initialize
and the destructor of "ostream" will output
Clean up

But I don't know if my idea is correct. Is there better idea? Thanks!
One simple way:

#include <iostream>

class Foo
{
public:
Foo() { std::cout << "Initialize\n"; }
~Foo() { std::cout << "Cleanup\n"; }
};

Foo Bar;

int main()
{
std::cout << "Hello, world!\n" ;
return 0;
}

Although I'm not sure what they're actually looking for.
Aug 4 '07 #2

P: n/a
On 2007-08-04 22:56, Jim Langston wrote:
"Guofu Chen" <gc***@mathworks.comwrote in message
news:f9**********@fred.mathworks.com...
>Hi,

In TC++PL, Chapter 10, there is an exercise which ask us to modify the
following program:

#include <iostream>

int main()
{
std::cout << "Hello, world!/n" ;
return 0;
}

so that the output would be:

Initialize
Hello, world!
Clean up

but we should not change main().

I believe we will have to modify the constructor and desctructor of class
"ostream" so that the constructor of "ostream" will output
Initialize
and the destructor of "ostream" will output
Clean up
Certainly not, you should never attempt to modify the standard library!
>But I don't know if my idea is correct. Is there better idea? Thanks!

One simple way:

#include <iostream>

class Foo
{
public:
Foo() { std::cout << "Initialize\n"; }
~Foo() { std::cout << "Cleanup\n"; }
};

Foo Bar;

int main()
{
std::cout << "Hello, world!\n" ;
return 0;
}

Although I'm not sure what they're actually looking for.
Chapter 10 is about classes, and I would assume that this exercise is
meant to teach about the lifetime for global objects.

--
Erik Wikström
Aug 4 '07 #3

P: n/a
Just as a matter of principle, wouldn't it be better like:

Jim Langston wrote:
#include <iostream>
namespace
{
class Foo
{
public:
Foo() { std::cout << "Initialize\n"; }
~Foo() { std::cout << "Cleanup\n"; }
};

Foo Bar;
}
>
int main()
{
std::cout << "Hello, world!\n" ;
return 0;
}
Aug 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.