Passing array to methode

T

Thomas Kowalski

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(unsigned char[3]& color);

How would the correct syntax look like?

Thanks in advance,
Thomas Kowalski
 
S

Stefan Naewe

Thomas said:
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(unsigned 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(Color & 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.
 
S

Stuart Redmann

Thomas said:
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(unsigned 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
 
T

Thomas Kowalski

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
 
G

Gianni Mariani

Thomas said:
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(unsigned 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.
 
M

Michiel.Salters

Thomas said:
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(unsigned 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(color& c); // returns c

HTH,
Michiel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top