Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ofstream

Reply
Thread Tools

ofstream

 
 
junw2000@gmail.com
Guest
Posts: n/a
 
      01-25-2008
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.
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      01-25-2008
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) {

....

 
Reply With Quote
 
 
 
 
junw2000@gmail.com
Guest
Posts: n/a
 
      01-25-2008
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.


 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      01-25-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
Extending ofstream class? Lars Yencken C++ 2 08-14-2003 11:53 PM
File cannot open with ofstream constructor ! tornado C++ 1 07-29-2003 04:38 PM
help: ofstream doesn't always create files? james545@my-deja.com C++ 0 07-16-2003 02:27 PM
writes to files passed as struct of ofstream objects fail m vaughn C++ 0 07-11-2003 01:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57