2
void foo (  int arg1, int arg2)

foo(1)

foo(1,2)

how do i know whether the int arg2 is using the optional function parameter that was passed like in the second foo call or the default one was used like in the 1st foo?

Update:

public:

header file

// Point 1
void fireworkShipment(stack<Firework*>&);
void sellFireworks(stack<Firework*>&, int quantity);

// Point 2
void metalShipment(stack<Metal>&);
~FireworkFactory();

// Point 3 for correctness, Point 4 for O(1) runtime
void sellFireworks(stack<Firework*>&, int quantity, Color color);
void sellFireworks(stack<Firework*>&, int quantity); //error here

cpp file:

    void FireworkFactory::sellFireworks(stack<Firework*>& customerStack, int quantity, Color color){
    // TODO
    cout<< "which color: " << color << "\n";
    for (int i = 0; i< fireworkstorage.size(); i++) {
        if (fireworkstorage.front()->getColor() == color && quantity != 0) {
            customerStack.push(fireworkstorage.front());
            cout<< "Color: " <<fireworkstorage.front()->getColor() << "\n";
            fireworkstorage.pop();
            quantity--;
            cout<<  "Quantity: " <<quantity << "\n";
        } else {
            fireworkstorage.push(fireworkstorage.front());
            fireworkstorage.pop();
        }
    }    
}

void FireworkFactory::sellFireworks(stack<Firework*>& customerStack, int quantity){
    for (int i = 0; i<quantity; i++) {
        customerStack.push(fireworkstorage.front());
        fireworkstorage.pop();
    }
}
DST
  • 71
  • 8

3 Answers3

4

You cannot. However, you may create overloaded function with one parameter:

#include <iostream>

void awesome_function_with_default_parameters(int arg1, int arg2 = 42)
{
    std::cout << "Doing some awesome work with " << arg1 << " and " << arg2 << std::endl;
}

void foo(int arg1, int arg2)
{
    std::cout << "Both parameters passed" << std::endl;
    awesome_function_with_default_parameters(arg1, arg2);
}

void foo(int arg1)
{
    std::cout << "Only one parameter passed" << std::endl;
    awesome_function_with_default_parameters(arg1);
}

int main()
{
    foo(1);
    foo(1, 314);
    return 0;
}
awesoon
  • 32,469
  • 11
  • 74
  • 99
1

There is no way to know. if foo(1) works then you assume that optional parameter is used.

sunny1304
  • 1,684
  • 3
  • 17
  • 30
0

There is, as others have already pointed out, no way to tell whether or not the default parameter was used, or a value specified explicitly. This is because default parameters are entirely handled at compile time and the program at run-time has no idea about them. So in theory you could have

void myFunc(int a, int b = 42);

in the header and in the cpp do

void myFunc(int a, int b) {
  bool defaultParamUsed = (b ==42);
  // rest of function
}

but this would mean that even someone providing the 42 explicitly would be deemed to have used the default parameter.

Just to make matters more complicated, because the compiler handles default parameters, you can quite nicely shoot yourself in the foot by providing different default parameter declarations for the same function signature. Consider in header a.h:

void myFunc(int a, int b=42);

In header b.h:

void myFunc(int a, int b=70);

and in the implementation file aMess.cpp:

void myFunc(int a, int b) // ...

The compiler would probably (I'm pretty sure but have not tried) complain (due to duplicate declaration) if both a.h and b.h were included anywhere, but different parts of your program could include different headers and thus work with different optional parameters.
It's hard to see under what circumstances one would want this. One case might be a function of the form

void doSomething(int param, int version);

Now different headers could specify different versions as default, and the user could still explicitly override the behaviour in case he or she requires the behaviour of a specific version.