2

Hi i have a confusion or to say more i need to understand something. I have a procedure and another overloaded procedure of same.

    string conct (string a, string b) {
      string str = conct(a, b, "string");
      return str;
    }

    string conct (string a, string b, const char* c) {
      // do the processing;
      return concatenated_string;
    }

is it possible that instead of having two overloaded functions, if i make c in the overloaded function as default argument. So that even if someone passes only two arguments, i can just have one function to handle that case.

But my main concern comes in the third argument which is currently const char* c. So if i make it to something like const char* c = "string", would it be correct way to handle the case of removing overloading with one function with default argument.

I saw the post here but that seems to be focused on compilation and not the confusion i have.

3
  • Yes. It is possible to use default argument Commented Jan 11, 2013 at 19:07
  • I also have a small question, function overloading helps improving maintainability. But does it affect the overall performance when the overloaded procedures are invoked very frequently. Commented Jan 11, 2013 at 19:12
  • 4
    Regarding your comment: function overloading is resolved at compile time, so it has absolutely no effect on runtime performance. Commented Jan 11, 2013 at 19:17

2 Answers 2

4

Yes, you can replace your overloaded functions with one function and a default argument:

string conct (string a, string b, const char* c = "string") {
  // do the processing;
  return concatenated_string;
}
  • When you overload functions the compiler generates code for each function, probably resulting in larger code size.
  • If an overload just acts as a thin wrapper as in your case then the optimizer may eliminate the extra work.
  • default arguments get set at the caller's location, rather than inside the function, so default arguments must be publicly visible, and changing them requires recompiling all callers. With an overload like yours the psuedo-default argument becomes a hidden detail.
Sign up to request clarification or add additional context in comments.

2 Comments

So generally speaking, whether i remove the first function and add the third arg as default in second, or not, it doesn't really helps in any performance in my case.
upvote for adding "default arguments get set at the caller's location, rather than inside the function". That explains why we need not specify default value in definition of the method
0

default values can be used in function prototypes but if we want to default middle argument then we'll have to default all values to its right... On the other hand overloading a function can be done for all possible argument combinations also default value needs not to be placed on function call stack and thus less work for the compiler...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.