1

I've been working on a C++ project for school and I've encountered some problems when it comes to 2D arrays of objects. So I have the following class:

class mecanice
{
public:

    mecanice();
    ~mecanice();
    ...
protected:

private:
    ...
    MyClass Ob[8][8];
};

And I need a method that will return Ob so that I can use it in a method from some other class that will do stuff based on what's in Ob:

class doodle
{
public:
    doodle();
    ~doodle();
    void do_stuff(MyClass M[8][8]);
    ...
protected:

private:
    ...
};

I've tried all kinds ways of going about it but all of them ended up with compiling errors usually related to pointers. Is there any way to do this?

2
  • Have you tried to use typedef MyClass MyClassArray[8][8]; and returning Ob as a MyClassArray? Commented Nov 19, 2016 at 23:02
  • Use std::vector Commented Nov 19, 2016 at 23:08

2 Answers 2

3

Try the following

const MyClass ( * myMagicMethod() )[8] const;

Or

typedef const MyClass ( *A )[8];

//...

A myMagicMethod() const;

The qualifier const is used if you do not want that the array would be changed in a method of other class.

Another approach is to pass the array by reference. For example

const MyClass ( & myMagicMethod() )[8][8] const;

Or

typedef MyClass ( &A )[8][8];

//...

const A myMagicMethod() const;

In the method that will process the array the parameter also must be declared like reference.

Sign up to request clarification or add additional context in comments.

3 Comments

Personally, I would use typedef MyClass (A)[8][8]; (or typedef decltype(Ob) A;), and have the function return const A&, to clarify that the function is returning a reference (and so the typedef can also be used to declare local variables, if necessary). That's just personal preference, though.
Thank you very much, I managed to get it to work now by just passing the array by reference. I didn't know about the sytanx (&MyMethod())[8][8] which is why I was getting all kinds of errors. Side question: why is there another const at the end? I get a compiler error if I leave it there (using CodeBlocks with MinGW that comes with it btw).
@GeorgeTodosi As I wrote in my answer you can simplify the syntax by using typedef.:)
0

You cannot return arrays from functions. In your case, a function that looks like:

MyClass[][] bar ();

is ill-formed.

You can consider to have that function take MyClass[][] as a parameter, instead of returning it as a return value, and filling it appropriately.

void bar (MyClass foo[8][8]);

You can also return a pointer instead (which is legal):

MyClass** bar () { }

But you will have to make sure that the memory of the object you are returning a pointer to is persistent (i.e. do not return a pointer to a local variable, etc...).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.