473,394 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Optional Parameters in Functions

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

Jul 19 '05 #1
2 9490
"Programmer" <js******@jsjholdings.com> 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
Jul 19 '05 #2
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:K0C0b.204068$Ho3.27244@sccrnsc03...
"Programmer" <js******@jsjholdings.com> 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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Phester | last post by:
Some of the functions that are part of PHP have optional parameters. Is it possible to define your own function so that it has optional parameters? If so, how is that done? Thanks, Phester ...
5
by: (Pete Cresswell) | last post by:
I've dabbled in "Optional" over the last few days and think I'm coming down against using it. Seems to me like it makes the code harder to read and more complicated. Instead of using Optional,...
13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
21
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code...
2
by: et | last post by:
I have the following function that uses an optional parameter, and it sets a default, as the program says I have to. Yet it doesn't pick up the default value. Why is this, what am I doing wrong....
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
5
by: Miro | last post by:
This qustion is probably for people who have created large apps with subs / or functions that have a lot of parameters and used in a lot of places in ur whole app. ( lets say its ur own library...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
6
by: Rob Hoelz | last post by:
So these two functions are different: void foo(void); and void foo(); because the first one allows no arguments, but the second does. My question is: In the implementation of the...
1
by: peridian | last post by:
This is more of a general question, but I didn't know where to post it. Since Java is an example of a language which does this, I thought here would work. Coming from a C++ background, having...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.