1

Hi I'm new to programming and I'm entering second semester, so please have patience with my question as it is not for an assignment I'm just trying to see how some things work.

I have a program I made which I wanted to demonstrate how to return a 2D array from a Class method. I was reading my text book and it says for the "get" methods to make them const so that the programmer does not inadvertantly make changes to the private variables.

Ok so with that out of the way, this is what I came up with for the method of a [10][10] 2 dimensional character array:

//private member is:
private:
      char array[10][10];

//method prototype
public:
      const char(* getArray()const )[10];

//and then the actual method itself
const char(* getArray()const )[10]
{
      return array;
}

This works but I'm wondering if this is proper code (I believe it is not due to the fact that it forced me to put const before char*, whereas all the examples of const usage in get methods I find online and on the book only have const at the end of the line) and why does it work anyway? I tried doing char (* getArray1())[10] const; but that does not work and the compiler complains that it expects a { before const that way but that isn't what I'm trying to do.

Also I know that if this was a 1D array, it would just simply be:

char* getArray() const;

because I tested it and it works. I saw another web page that stated how to use a 1D array in place of the 2D array so I'm already aware of that, however that will basically make a new array of 100 (array[100], which basically is array[10 * 10])

I have looked at other examples on here and I am also aware of the fact that this possibly returns what is called a "decayed array", among other things such as possible bad coding technique etc.

But if I got this right, let me know. If not, let me know what you think.

thanks

1 Answers1

0

Try typedef char const (*array_t)[10]; array_t getArray() const.

a.l.
  • 1,085
  • 12
  • 29
  • That looks very good and it looks like it should work, but now the compiler is saying, for the return member, Error: return value type does not match the function type. I was seeing this error earlier when I was trying some other things earlier too. – user2426847 May 28 '13 at 03:58
  • And if I add const between typedef and char, it alleviates that again but is that really proper code? – user2426847 May 28 '13 at 04:05
  • Did you just return `array` without type-convert? Try `typedef char const (*array_t)[10]` instead. The type of `array` is `char [10][10]`, when using it as a pointer, it's `char const (*)[10]` while `array_t` didn't contain the `const` qualifier. – a.l. May 28 '13 at 06:40
  • Hi that looks like it works. Thanks. Good thing I learned a smidge about what typedef is from what I read in the book for the course so after you showed me this, I understand what that does. Basically you're creating a new type called array_t by using typedef. Thanks. I'm amazed this solution is a lot shorter than I expected on here and very concise. – user2426847 May 28 '13 at 19:44