In message
<9b9eb229-28ce-4c80-93d1->,
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