Default Value while 'passing by reference'

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

    Default Value while 'passing by reference'

    We know C++ supports functions that can carry default values as
    follows:

    someFunction(in t a, int b, int c =20);

    So calling someFunction(x, y) is perfectly legal!

    However, if I need to information by reference, can initiate the
    referenced variable to a default value?

    Eg:

    int main()
    {
    int a = 20;
    int b, c;

    int &r = a;

    someFunction(b, c); // <-------------------- POSSIBLE??
    someFunction(b, c,r);//<-------------------- This is allowed

    }

    someFunction(in t x, int y, int& z = 30); //<-- WELL??
    {
    }

  • Sharad Kala

    #2
    Re: Default Value while 'passing by reference'


    "mufasa" <soso7df@gmail. com> wrote in message[color=blue]
    > We know C++ supports functions that can carry default values as
    > follows:
    >
    > someFunction(in t a, int b, int c =20);
    >
    > So calling someFunction(x, y) is perfectly legal!
    >
    > However, if I need to information by reference, can initiate the
    > referenced variable to a default value?
    >
    > Eg:
    >
    > int main()
    > {
    > int a = 20;
    > int b, c;
    >
    > int &r = a;
    >
    > someFunction(b, c); // <-------------------- POSSIBLE??
    > someFunction(b, c,r);//<-------------------- This is allowed
    >
    > }
    >
    > someFunction(in t x, int y, int& z = 30); //<-- WELL??[/color]

    There should be no semicolon at the end
    [color=blue]
    > {
    > }[/color]

    This is not legal C++. You are binding a non const reference (z) to a
    temporary object, which is illegal. It will work if you bind it to some
    l-value.

    int a = 30;

    void
    someFunction(in t x, int y, int& z = a)
    {
    }

    Sharad






    Comment

    • Rolf Magnus

      #3
      Re: Default Value while 'passing by reference'

      mufasa wrote:
      [color=blue]
      > We know C++ supports functions that can carry default values as
      > follows:
      >
      > someFunction(in t a, int b, int c =20);
      >
      > So calling someFunction(x, y) is perfectly legal!
      >
      > However, if I need to information by reference, can initiate the
      > referenced variable to a default value?[/color]

      Yes.
      [color=blue]
      > int main()
      > {
      > int a = 20;
      > int b, c;
      >
      > int &r = a;
      >
      > someFunction(b, c); // <-------------------- POSSIBLE??
      > someFunction(b, c,r);//<-------------------- This is allowed
      >
      > }
      >
      > someFunction(in t x, int y, int& z = 30); //<-- WELL??[/color]

      Your function is missing a return type.
      [color=blue]
      > {
      > }[/color]

      Make that:

      void someFunction(in t x, int y, const int& z = 30); //<-- WELL??
      {
      }

      Comment

      • Gianni Mariani

        #4
        Re: Default Value while 'passing by reference'

        mufasa wrote:[color=blue]
        > We know C++ supports functions that can carry default values as
        > follows:[/color]
        ....[color=blue]
        >
        > someFunction(in t x, int y, int& z = 30); //<-- WELL??[/color]

        void someFunction(in t x, int y, const int& z = 30)[color=blue]
        > {
        > }
        >[/color]

        If you're passing a reference to a literal (30), does it make sense that
        you can modify it ? The C++ standard says you can't.

        G

        Comment

        Working...