Function prototype with optional parameters?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joerg Toellner

    Function prototype with optional parameters?

    Hi Group,

    sorry, its again me with a dumb question.

    If i have a function with the optional parameter "mode" like this:

    void myfunc(char *blah, int mode = NULL)
    {
    if(mode == NULL)
    do_something();
    else
    do_something_el se();
    }

    and i want to have a prototype for this function in my header-file and so i
    write:

    void myfunc(char *blah, int mode = NULL);

    I get a compile error: redefenition of mode

    If i write:

    void myfunc(char *blah, int mode);

    and i use the function with a function call like this:

    myfunc("Hello World");

    I got a compile error: function does not take 1 Parameter.

    But i want to have the mode-param. optional. So you can call the function
    with two parameters or with one and using the default value for "mode".

    How have i to do this to keep the compiler (and me :-)) ) happy?

    Someone out there who is willing to enlighten me a little bit? Thanks a
    loooot!
    CU
    Joerg Toellner


  • Jakob Bieling

    #2
    Re: Function prototype with optional parameters?

    "Joerg Toellner" <toellner@oss-gmbh.de> wrote in message
    news:bf37ti$bs1 $01$1@news.t-online.com...
    [color=blue]
    > If i have a function with the optional parameter "mode" like this:
    >
    > void myfunc(char *blah, int mode = NULL)
    > {
    > if(mode == NULL)
    > do_something();
    > else
    > do_something_el se();
    > }
    >
    > and i want to have a prototype for this function in my header-file and so[/color]
    i[color=blue]
    > write:
    >
    > void myfunc(char *blah, int mode = NULL);
    >
    > I get a compile error: redefenition of mode[/color]

    Right, because the default value for the optional argument shall onl
    appear once. If you have a function prototype, the default argument goes
    there, and only there. Like this:

    void myfunc(char *blah, int mode = NULL);

    void myfunc(char *blah, int mode)
    {
    if(mode == NULL)
    do_something();
    else
    do_something_el se();
    }

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)


    Comment

    • Jan Rendek

      #3
      Re: Function prototype with optional parameters?

      >[color=blue]
      > If i have a function with the optional parameter "mode" like this:
      >
      > void myfunc(char *blah, int mode = NULL)
      > {
      > if(mode == NULL)
      > do_something();
      > else
      > do_something_el se();
      > }
      >
      > and i want to have a prototype for this function in my header-file and so i
      > write:
      >
      > void myfunc(char *blah, int mode = NULL);
      >[/color]

      Define the default param value in the header:

      ..hpp: void myfunc(char *blah, int mode = NULL);

      but NOT in your source file:

      ..cpp: void myfunc(char *blah, int mode) { /* WHATEVER */ }


      Take no offence, but by the look of your questions, you ought to get
      a decent C++ book. Everything you asked so far should be in the first
      chapters.

      regards,

      Jan

      --
      Jan Rendek
      INRIA Lorraine
      r e n d e k @ l o r i a . f r

      Comment

      • Joerg Toellner

        #4
        Re: Function prototype with optional parameters?

        Hi Jakob,

        AAAAAAAH! I seeeee!
        *SomeoneSwitche sABigLightBulbO verMyHeadToOnSt ate*

        Thanks a lot, that makes sense!
        Why haven't not thought about that by myself? *ShameRedChangi ngFaceColor*

        Thx. again...and have a nice day!
        CU
        Joerg Toellner

        "Jakob Bieling" <netsurf@gmy.ne t> schrieb im Newsbeitrag
        news:bf38ga$cd8 $01$2@news.t-online.com...[color=blue]
        > Right, because the default value for the optional argument shall onl
        > appear once. If you have a function prototype, the default argument goes
        > there, and only there. Like this:
        >
        > void myfunc(char *blah, int mode = NULL);
        >
        > void myfunc(char *blah, int mode)
        > {
        > if(mode == NULL)
        > do_something();
        > else
        > do_something_el se();
        > }
        >
        > hth
        > --
        > jb[/color]


        Comment

        • Joerg Toellner

          #5
          Re: Function prototype with optional parameters?

          Hi Peter,

          Wow! What an Idea with the function overloading. I know function overloading
          and used it where i have different Inputs for the same function. But never
          thought of this usage for defaulting parameters. But it make sense.

          I'll look, if i can get the recommended book.

          TYVM
          Joerg Toellner

          "Peter van Merkerk" <merkerk@deadsp am.com> schrieb im Newsbeitrag
          news:bf39jp$ajo k7$1@ID-133164.news.uni-berlin.de...[color=blue]
          > See above. Alternatively you could create an overloaded variant of the
          > function.
          >
          > --------foo.h-----------------
          > void myfunc(char *blah);
          > void myfunc(char *blah, int mode);
          >
          > --------foo.cpp-----------------
          > void myfunc(char *blah)
          > {
          > do_something();
          > }
          >
          > void myfunc(char *blah, int mode)
          > {
          > do_something_el se();
          > }
          >
          > Item 24 of the book "Effective C++" from Scott Meyers (highly
          > recommended) discusses how to choose beteen function overloading and
          > parameter defaulting.[/color]


          Comment

          Working...