A question in TC++PL!

G

Guofu Chen

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!
 
J

Jim Langston

Guofu Chen said:
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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Certainly not, you should never attempt to modify the standard library!
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.
 
M

Mike Wahler

Guofu Chen said:
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"

I seriously doubt that. You have no guarantee that
you have access to the source code of the 'ostream' class.
And even if you do, imo it's not a good idea to modify
standard library entities.
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!

I'd create my own class, and a file-scope instance of it.
That class' constructor and destructor would emit the
'Initialize' and 'Clean up' messages.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top