ofstream



 DEVELOP > c-Plus-Plus > ofstream

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 25 Jan 2008 12:55:22 AM
Object: ofstream
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.
.

User: "Ron Natalie"

Title: Re: ofstream 25 Jan 2008 01:42:00 AM
wrote:

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) {
....
.
User: ""

Title: Re: ofstream 25 Jan 2008 02:22:54 AM
On Jan 24, 11:42 pm, Ron Natalie <r...@spamcop.net> wrote:

junw2...@gmail.com wrote:

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) {

But reference can not be reassigned. Can I pass a ofstream to
MyFunction as below?
ofstream outStream("myfile");
MyFunction ( 10, 100,outStream );
Thanks.
.
User: "Richard Herring"

Title: Re: ofstream 25 Jan 2008 05:14:08 AM
In message
<9b9eb229-28ce-4c80-93d1-f13485bd810a@f47g2000hsd.googlegroups.com>,
junw2000@gmail.com writes

On Jan 24, 11:42 pm, Ron Natalie <r...@spamcop.net> wrote:

junw2...@gmail.com wrote:

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) {

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.


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

--
Richard Herring
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER