Click to See Complete Forum and Search --> : Return an array from a class method


nubira
11-03-2005, 03:49 PM
Hi all

Sorry if this seems very simple and obvious to you. I would appreciate it a lot if you could help me and describe how to do this properly.

I am trying to figure out how to return an unsigned char array from a class method. The class is just a practice how to store RGB color values as an object. I chose unsigned char for the type because it is enough to represent RGB values from 0 to 255.

The header file of the class looks like this:


#ifndef _CARRAYRGB_H_
#define _CARRAYRGB_H_

class CArrayRGB
{
private:

unsigned char values[3];

public:

CArrayRGB();
CArrayRGB( unsigned char R, unsigned char G, unsigned char B );
~CArrayRGB() {}

void setColor( unsigned char R, unsigned char G, unsigned char B );
unsigned char* getColor();
};
#endif


The class method file looks like this:


#include "CArrayRGB.h"

CArrayRGB::CArrayRGB()
{
values[0] = 255;
values[1] = 255;
values[2] = 255;
}

CArrayRGB::CArrayRGB( unsigned char R, unsigned char G, unsigned char B )
{
values[0] = R;
values[1] = G;
values[2] = B;
}

void CArrayRGB::setColor( unsigned char R, unsigned char G, unsigned char B )
{
values[0] = R;
values[1] = G;
values[2] = B;

}

unsigned char* CArrayRGB::getColor()
{
return values;
}


...and in the client file I have this (well actually it's quite empty):



//blah blah, then include class header:

#include "CArrayRGB.h"

// blah blah, then:

CArrayRGB rgbarr; // create object which holds RGB

// BEEP! What do I do here to get the RGB in form 255,255,255
// which I can then pass to whatever needs the RGB values...?

// using rgbarr.getColor(); somehow but, how?

// blah, blah, program end.


As you can see this is very basic but still, I can't figure it out myself. I am guessing my class header file and method file are not quite right... And because they are not right, I can't continue to write the client file because I have no clue how to get the RGB values from the object in form 255,255,255 so that I can pass them to whatever needs them.

Corrections, improvements, suggestions etc. are very, very welcome.

Thanks in advance for guiding me through what I did wrong and how to do it properly.

jonnin
11-03-2005, 03:57 PM
char* CArrayRGB:: returnme()
{
return values; //return val must not be 'destroyed' (class member or static are fine)
}

remember that

x = returnme();
x[0] = 1; //array notation works here.

nubira
11-03-2005, 04:01 PM
So I am 'destroying' the return value? Hmm, I have really no clue. How would I do it properly then? Can I use const somehow to make it right?

jonnin
11-03-2005, 04:10 PM
This would destroy the return value:

char * foo()
{
char goner[10];
return goner; //uh-oh! goner is gone when foo exits!
}

the earlier example should be fine -- the object variable ('values') is always in existence if you can use (the object's) member function.

Danny
11-03-2005, 06:36 PM
The short answer is that you can't return an array in C++. The closest you can get is returning a pointer but this is often a painful thing because where will the array be allocated? On the callee's stack? Bad idea! on the heap? Bad also!
The best thing in your case would be to return void and change the prototype of the function so that it takes a pointer as an argument. That pointer should point to an empty array, so the callee will simply write values to that array. Alternatively, pack the three chars in a struct and return that struct by value. You can also use a vector but that's a bit overkill since your arrays contains only three bytes.

nubira
11-04-2005, 02:53 PM
Thank you both for helping me. I have yet to try your suggestions but it sounds like they could do the trick.

:)

If anybody else has additional information on the subject, please do not hesitate to tell it here.