|
|
ofstream #2
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.
|
|
0
|
|
|
|
Reply
|
junw2000 (221)
|
1/25/2008 6:55:22 AM |
|
junw2000@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) {
....
|
|
0
|
|
|
|
Reply
|
ron131 (898)
|
1/25/2008 7:42:00 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.
|
|
0
|
|
|
|
Reply
|
junw2000 (221)
|
1/25/2008 8:22:54 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
|
|
0
|
|
|
|
Reply
|
Richard
|
1/25/2008 11:14:08 AM
|
|
|
3 Replies
207 Views
(page loaded in 0.094 seconds)
|
|
|
|
|
|
|
|
|