ofstream

J

junw2000

How to assign a default arguement to ofstream?
For example:

void My_Function( int a, int b = 0, ofstream outFile )
{
//Boby of the code
}

The arguement b has a default value 0. How to assign a default
arguement to the arguement outFile so that the caller of the function
does not need to pass a value to it?

Thanks.
 
R

Ron Natalie

How to assign a default arguement to ofstream?
For example:

void My_Function( int a, int b = 0, ofstream outFile )
{
//Boby of the code
}

The arguement b has a default value 0. How to assign a default
arguement to the arguement outFile so that the caller of the function
does not need to pass a value to it?

Thanks.

You can't pass streams by value. They don't have copy constructors
but you can do it by reference.

void MyFunction (int a , int b = 0, ofstream& outFile = cout) {

....
 
J

junw2000

You can't pass streams by value. They don't have copy constructors
but you can do it by reference.
void MyFunction (int a , int b = 0, ofstream& outFile = cout) {

But reference can not be reassigned. Can I pass a ofstream to
MyFunction as below?

ofstream outStream("myfile");
MyFunction ( 10, 100,outStream );

Thanks.
 
R

Richard Herring

In message

If the function just writes to the stream and doesn't do anything
specifically file-related, you could make it more general by using
ostream &.
But reference can not be reassigned. Can I pass a ofstream to
MyFunction as below?

Yes. You're not reassigning it - you initialise a new reference whose
scope is the body of the function, each time the function is called.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top