473,385 Members | 1,373 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,385 software developers and data experts.

Optional method arguments

46
Hey,

If you don't want to read all this, just look at the bottom part :-P

I'm currently making a Tic Tac Toe game, just for fun, and ran across an interesting subject(for me). I have a method in class Player that can be used for two uses. It can check for a win and check for a possible win. Once it finds a possible win, it puts the X and Y coords of that possible win into a variable, located in the class. But what if I wanted to fulfill a second use of this function and store those X and Y variables in a different class instance of Player, allowing the other player to block that attack? That is to say, this way the function can be used to see if a player can possibly win, or if a player can possibly block a win, and give the exact coordinates of these events. Ok, onto the point: In order to do this, my first idea was to include an optional parameter.

So the prototype would be:
Expand|Select|Wrap|Line Numbers
  1. checkForWin(board& gameBoard, player &opponent);
  2.  
With opponent as the optional paramater. In short, how do I make that parameter optional?

Hope this was clear.

Thanks,
Austen
May 13 '07 #1
1 1255
Hi,

You can change your func signature as

checkForWin(board& gameBoard, player *opponent = 0);

this way, you can pass in the player if needed(for the second use of the function). You can also use the following signature

checkForWin(board& gameBoard, player& opponent = player());

but its not normally used, b'cos it creates an object (which may/may not be costly, depends on the object).


Regards
Prasannaa.
May 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Philip Rittenhouse | last post by:
I have discovered a couple of problems with the way the universal gateway code handles optional parameters and variable argument lists in COM servers. It appears to only be a problem when you...
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. ...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
7
by: An Jiye | last post by:
Hi. I have an ActiveX control created by MFC. One method has the following definition: SHORT MyMethod(BSTR p1, VARIANT p2, VARIANT p3); In VB6, I can call this method by the following...
12
by: Nick Hounsome | last post by:
Can anyone tell me what the rational is for not supporting optional arguments. It is obviously a trivial thing to implement and, since C++ has them, I would not expect them to be omitted without...
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...
1
by: info | last post by:
I need to use DAO in one of my applications. It was too hard to pass through by optional parameters for several methods, but now I'm totally lost at DAO seek method (the main method for key s)....
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: Glenn | last post by:
I am writing a Web Service in C# ASP.NET 2.0 that takes two optional parameters. One is an int and the other is a string. Below is the definition of the Web Method: public...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.