DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2005

    Return an array from a class method

    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
            unsigned char values[3];
            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();    
    The class method file looks like this:

    #include "CArrayRGB.h"
            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.

  2. #2
    Join Date
    Dec 2003
    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.
    Last edited by jonnin; 11-03-2005 at 04:42 PM.

  3. #3
    Join Date
    Nov 2005
    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?

  4. #4
    Join Date
    Dec 2003
    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.
    Last edited by jonnin; 11-03-2005 at 04:40 PM.

  5. #5
    Join Date
    Nov 2003
    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.
    Danny Kalev

  6. #6
    Join Date
    Nov 2005
    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.

Similar Threads

  1. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  2. Replies: 2
    Last Post: 04-15-2005, 09:06 PM
  3. StudentList Class
    By WillisTi in forum Java
    Replies: 2
    Last Post: 02-25-2005, 06:35 AM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.