473,785 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,bo ol);
void Write(float,boo l);
void Write(int,bool) ;
void Write(double,bo ol);
void Write(char,bool );
~Debug();

private:
ofstream outDebug;
};

And my definition of one of these looks like this:
void Debug::Write(st ring 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 9507
"Programmer " <js******@jsjho ldings.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,bo ol);
Change this to

void Write(string, bool = true);
void Write(float,boo l);
void Write(int,bool) ;
void Write(double,bo ol);
void Write(char,bool );
~Debug();

private:
ofstream outDebug;
};

And my definition of one of these looks like this:
void Debug::Write(st ring 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(st ring 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.********@att Abi.com> wrote in message
news:K0C0b.2040 68$Ho3.27244@sc crnsc03...
"Programmer " <js******@jsjho ldings.com> wrote...
And my definition of one of these looks like this:
void Debug::Write(st ring 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(st ring 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
102444
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 vilefesteringscum at
5
9505
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, I'm making the parm a Variant and passing Null when it's not used.... then checking for "If Len(theParmValue & "") > 0... to see if it is present. That way I can eliminate checks for IsMissing(theParmValue)...
13
2517
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. I've done this before with Modules, and saw that <gasp> they behave just like sealed classes with only static members. Anyway, it looks like Optional Parameters are nothing but a way to tell the compiler to write some overloads for you. So, in...
21
5772
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 would go something like this: dim qry as dao.querydef set qry = currentdb.querydefs("myquery") qry.parameters("Par1") = "blah"
2
1656
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. Public Function SelectQuery(strTbl as string, arr as ArrayList, Optional SortCol as string = "Yr desc") Calling procedure: qry = SelectQuery(strTbl, arr, SortCol)
14
3275
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 make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
5
1410
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 function ) -Not looking for a big complicated question... ( dont waste a lot of ur time please ) I have been searching for the "Optional" parameter forever in VB.net I have found a pretty down to earth article on why NOT to use them....
12
2674
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 avoided. I'd like to hear your various opinions on this matter.
6
2710
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 second function, how does
1
8419
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 used VBA/VB6 quite a bit, and a number of scripting languages with similar traits, like php, I had grown accustomed to optional parameters in my functions. I recently found out that certain modern languages do not support these, namely Java and...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.