Passing array to methode

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

    Passing array to methode

    Hi everyone,
    recently I have trouble to pass an array (unsigned char color[3]) to a
    methode by reference and collect the return value by reference.

    I wanted something like:
    unsigned char[3]& changeColor(uns igned char[3]& color);

    How would the correct syntax look like?

    Thanks in advance,
    Thomas Kowalski

  • Stuart Redmann

    #2
    Re: Passing array to methode

    Thomas Kowalski wrote:
    Hi everyone,
    recently I have trouble to pass an array (unsigned char color[3]) to a
    methode by reference and collect the return value by reference.
    >
    I wanted something like:
    unsigned char[3]& changeColor(uns igned char[3]& color);
    >
    How would the correct syntax look like?
    >
    I suppose that this is a point where you can get around a typedef.

    typedef unsigned char CharArray[3];
    CharArray& changeColor (CharArray& p_Color)
    {
    return p_Color;
    }

    Regards,
    Stuart

    Comment

    • Stefan Naewe

      #3
      Re: Passing array to methode

      Thomas Kowalski schrieb:
      Hi everyone,
      recently I have trouble to pass an array (unsigned char color[3]) to a
      methode by reference and collect the return value by reference.
      >
      I wanted something like:
      unsigned char[3]& changeColor(uns igned char[3]& color);
      If you pass the array to the function by reference, why
      do you wnat to return it as a reference as well ?
      How would the correct syntax look like?
      I'd use a typedef:

      typedef unsigned char Color[3];

      Color& changeColor(Col or & col)
      {
      return col;
      }

      int main()
      {
      Color c;
      Color& b = changeColor(c);
      return 0;
      }


      But I really don't understand what you want to achieve with this.

      S.
      --
      Stefan Naewe
      stefan_DOT_naew e_AT_atlas_DOT_ de

      Comment

      • Thomas Kowalski

        #4
        Re: Passing array to methode

        typedef unsigned char Color[3];

        If you see it, it's quite simple (hit my head). Thanks
        But I really don't understand what you want to achieve with this.
        Actually I mixed two different methodes.
        I have a getter and and change methode but I combined them to keep the
        text short.

        Thanks,
        Thomas Kowalski

        Comment

        • Gianni Mariani

          #5
          Re: Passing array to methode

          Thomas Kowalski wrote:
          Hi everyone,
          recently I have trouble to pass an array (unsigned char color[3]) to a
          methode by reference and collect the return value by reference.
          >
          I wanted something like:
          unsigned char[3]& changeColor(uns igned char[3]& color);
          unsigned char (& changeColor( unsigned char ( & color )[3] ) )[3]
          {
          return foo;
          }
          >
          How would the correct syntax look like?
          The typedef answers are better but if you need to create a template,
          you're likely going to need to use that syntax.

          Comment

          • Michiel.Salters@tomtom.com

            #6
            Re: Passing array to methode


            Thomas Kowalski wrote:
            Hi everyone,
            recently I have trouble to pass an array (unsigned char color[3]) to a
            methode by reference and collect the return value by reference.
            >
            I wanted something like:
            unsigned char[3]& changeColor(uns igned char[3]& color);
            >
            How would the correct syntax look like?
            struct color {
            unsigned char components[3];
            color& changeColor(); // return *this after modification.
            };

            And the free function:
            color& changeColor(col or& c); // returns c

            HTH,
            Michiel

            Comment

            Working...