0

Is it possible to make it so that you don't have to overload a function definition 3 times if you want to be able to pass between 1 to 3 parameters into it?

For example instead of:

function(class a) { //this is called if only 1 class is passed
instance 1 = a;
} 
function(class a, class b) {  //this is called if 2 classes are passed
instance1 = a;
instance2 = b;
}
function(class a, class b, class c) { //this is called if 3 classes are passed
instance1 = a;
instance2 = b;
instance3 = c;
}

You could have:

function(class a, <if2class>class b, <if3class>class c) //this is called
                                                        //for all passes
// <ifxclass> is not a real command, I'm using it for demonstration
{
    instance1 = a; //always
    instance2 = b; //if class b is passed
    instance3 = c; //if class c is passed
}

For the function call...

function(first, second) //example of a function call with this code

EDIT: explanation for a real use:

bossbattle(biglion, bigtiger);
bossbattle(biglion);
bossbattle(hades, biglion, biglion);
//where inside bossbattle it sets the classes passed in to temporary classes
//for battle. that's why I need a variable number of parameters

I have already created a battle system for normal enemies and it calls a function to randomly populate 1-3 spots based on a tiered percentage with random normal enemies. I'm trying to use that same battle system but with a different function (i.e. boss battle()) to populate the battle system with a boss fight. The class instances that the temporary enemies use for the battle are in an array named Enemy battlefield monsters[3] and I have a boolean isalive in each instance of the array which I want to be set to true if parameters are called in bossbattle() So for example there will be 3 isalive = true if there are 3 parameters passed into bossbattle() but only 1 set to true if only 1 parameter is passed.

LihO
  • 41,190
  • 11
  • 99
  • 167
Scott
  • 1,154
  • 1
  • 12
  • 25
  • 5
    function with default parameters? – billz Oct 19 '13 at 23:33
  • 1
    Please post a real usage situation. According to the zero-one-many rule, you should never have anything like "instance2" and "instance3" in the first place. – Kerrek SB Oct 19 '13 at 23:34
  • Look into how functions like printf work. I believe there is an operator similar to `...`. Ive seen this used in the definition ie. `printf(char *format, ...)`, but ive havent looked into it at all. – Shade Oct 19 '13 at 23:35
  • 1
    @Shade: That's called a [variadic function](https://en.wikipedia.org/wiki/Variadic_function), but that wouldn't work in this case unless the value of `a` (or some other variable that's always accessible) can be used to determine how many arguments were passed. – jwodder Oct 19 '13 at 23:37
  • The code that you want code "instead of" doesn't compile. You might not think this is important, but it renders your question nonsensical since you seem to be asking how to pass a class to a function, which is not possible in C++. If you're asking about code, ask about code that compiles. Then answerers can know that their answer really does replace what you have, instead of guessing on the basis of the most likely sensible think that you might mean. – Steve Jessop Oct 19 '13 at 23:37
  • billz: that might be an option, but then I would need to use quite a few if statements in the function definition right? I'm looking for something that would be more streamlined, like --> (class a, $class b, $class c) so the compiler knows that maybe the parameter is not passed and if not don't worry about it. --> $ being the made up conditional operator. – Scott Oct 19 '13 at 23:38
  • @Scott: What do you mean by "compiler knows that maybe the parameter is not passed and if not don't worry about it"? I would rather say: "let the compiler do its job and don't worry about what it is actually doing unless it's really necessary". – LihO Oct 19 '13 at 23:42
  • @KerrekSB updated, you can see why I think that it's needed – Scott Oct 19 '13 at 23:42
  • @SteveJessop I'm not passing a class, I'm passing the instances of the class. Sorry for the confusion. – Scott Oct 19 '13 at 23:47
  • @jwodder & shade This sounds what I am probably looking for. I edited the question to include a real life example. Could you kindly provide a syntax example for me? – Scott Oct 20 '13 at 00:10
  • @Scott: Tomáš Zíma already provided a syntax example in his answer. – jwodder Oct 20 '13 at 00:12
  • @jwodder I read this, and it says that this is not safe and it is not a varadic template. Am I missing something? – Scott Oct 20 '13 at 00:18
  • @Scott: Nope. Variadic functions aren't safe, and they're not templates. – jwodder Oct 20 '13 at 00:21

1 Answers1

7

Use default parameters to achieve this. Something like:

void foo(int arg, int arg2 = 0, int arg3 = 0) {
    ...
}

which allows you to call foo and pass either 1, 2 or 3 arguments to it.


Since you've mentioned your actual intentions: i.e. call "bossbattle(hades, biglion, biglion); where inside bossbattle it sets the classes passed in to temporary classes for battle".

In case this bossbattle is constrained with the amount of (let's call it) battle participants (i.e. there will always be 1 to 3 participants), then you can use the aforementioned approach. Otherwise it might be more reasonable to define a class that extracts whatever "hades", "lion" and "tiger" have in common to define the base class, let's say class Creature, and change the interface to take a container of creatures instead:

void bossbattle(const std::vector<Creature>& battleParticipants) {
    ...
}
LihO
  • 41,190
  • 11
  • 99
  • 167
  • would I need to use quite a few if statements in the function definition right? I'm looking for something that would be more streamlined, like --> (class a, $class b, $class c) so the compiler knows that maybe the parameter is not passed and if not don't worry about it. --> $ being the made up conditional operator. Check out the real example I added to the question for Kerrek SB maybe? – Scott Oct 19 '13 at 23:44
  • After reading through all the examples provided, I'm concluding that default parameters is the most efficient way to go. Everything else isn't really doing what I need it to do. Thanks! – Scott Oct 20 '13 at 00:30
  • @Scott: I'm glad I helped somehow :) – LihO Oct 20 '13 at 00:37