Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Optional Parameters in Functions

Reply
Thread Tools

Optional Parameters in Functions

 
 
Programmer
Guest
Posts: n/a
 
      08-20-2003
This may sound easy and it probably is, I just started learning C++.

I have a class that writes to a file and I would like to add a second
optional parameter that will be used to tell the class if a newline
character should be written so the next output will start on a new line.

My class looks like this:
class Debug {
public:
Debug();
void Open(string);
void Write(string,bool);
void Write(float,bool);
void Write(int,bool);
void Write(double,bool);
void Write(char,bool);
~Debug();

private:
ofstream outDebug;
};

And my definition of one of these looks like this:
void Debug::Write(string sDebug, bool bNewLine = true) {

if(bNewLine) {
outDebug << sDebug << endl;
} else {
outDebug << sDebug;
}
}

I want to set it up so that by default, it will write a new line, but it can
be overridden by sending a 'false' as the second parameter. However, I am
getting a "Write: no overloaded function takes 1 parameters" error when I
try to compile. The call looks like this:

DebugFile.Write("Debug File Opened");

I could write it like this, DebugFile.Write("Debug File Opened",true), but
since true is the default and the mode I will use 90% of the time, I was
hoping to be able to default to true so my calls will be shorter.

Any ideas?

Thanks in advance

John



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-20-2003
"Programmer" <(E-Mail Removed)> wrote...
> This may sound easy and it probably is, I just started learning C++.
>
> I have a class that writes to a file and I would like to add a second
> optional parameter that will be used to tell the class if a newline
> character should be written so the next output will start on a new line.
>
> My class looks like this:
> class Debug {
> public:
> Debug();
> void Open(string);
> void Write(string,bool);


Change this to

void Write(string, bool = true);

> void Write(float,bool);
> void Write(int,bool);
> void Write(double,bool);
> void Write(char,bool);
> ~Debug();
>
> private:
> ofstream outDebug;
> };
>
> And my definition of one of these looks like this:
> void Debug::Write(string sDebug, bool bNewLine = true) {


The default argument values should be used in the declaration,
not the definition. Remove it from here. Leave this as

void Debug::Write(string sDebug, bool bNewLine) {

.. Also, passing a string by value is inefficient. Passing it by
a reference to const is better.

>
> if(bNewLine) {
> outDebug << sDebug << endl;
> } else {
> outDebug << sDebug;
> }
> }
>
> I want to set it up so that by default, it will write a new line, but it

can
> be overridden by sending a 'false' as the second parameter. However, I am
> getting a "Write: no overloaded function takes 1 parameters" error when I
> try to compile. The call looks like this:
>
> DebugFile.Write("Debug File Opened");
>
> I could write it like this, DebugFile.Write("Debug File Opened",true), but
> since true is the default and the mode I will use 90% of the time, I was
> hoping to be able to default to true so my calls will be shorter.
>
> Any ideas?


See above.

Victor


 
Reply With Quote
 
 
 
 
Alan Sung
Guest
Posts: n/a
 
      08-20-2003
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:K0C0b.204068$Ho3.27244@sccrnsc03...
> "Programmer" <(E-Mail Removed)> wrote...
> > And my definition of one of these looks like this:
> > void Debug::Write(string sDebug, bool bNewLine = true) {

>
> The default argument values should be used in the declaration,
> not the definition. Remove it from here. Leave this as
>
> void Debug::Write(string sDebug, bool bNewLine) {
>
> . Also, passing a string by value is inefficient. Passing it by
> a reference to const is better.
>
> >
> > if(bNewLine) {
> > outDebug << sDebug << endl;
> > } else {
> > outDebug << sDebug;
> > }
> > }
> >
> > I want to set it up so that by default, it will write a new line, but it

> can
> > be overridden by sending a 'false' as the second parameter. However, I

am
> > getting a "Write: no overloaded function takes 1 parameters" error when

I
> > try to compile. The call looks like this:
> >
> > DebugFile.Write("Debug File Opened");
> >
> > I could write it like this, DebugFile.Write("Debug File Opened",true),

but
> > since true is the default and the mode I will use 90% of the time, I was
> > hoping to be able to default to true so my calls will be shorter.
> >
> > Any ideas?

>
> See above.
>
> Victor


I should also add:

void Write(const char *,bool=true);
in order to handle quoted strings so that you don't have to construct a
temporary string.

-al sung
Rapid Realm Technology, Inc.
Hopkinton, MA


 
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
SQLCommand with Optional Parameters? Hanover ASP .Net 1 09-28-2005 08:36 PM
Optional parameters in stored procedure call yuliad@scopus.net C++ 1 12-09-2003 02:01 PM
Q: How can I pass UDTs into optional parameters in VB 6.0 ? Ramesh MCSD 3 10-22-2003 06:22 PM
Optional parameters in a VB.net function Do ASP .Net 1 10-16-2003 11:50 AM
Function prototype with optional parameters? Joerg Toellner C++ 4 07-16-2003 10:48 AM



Advertisments