0

I have got a structure

class pyatno {


int pyatnoNumber;
int locX, locY;
bool possible;
char *number;
char pyatnoView[4][4];
}

the idea is to make a function, that would return an array of pyatno.pyatnoView objects, but there is a mess. I don't understand how exactly I can get access to this "property". I am not strong in c++, so if it isn't real, or i am talking something wrong, explain please, cause I am really stacked in this question.

5
  • 1
    You want to return a reference to the pyatnoView array, perhaps? Commented Mar 6, 2015 at 22:26
  • 2
    You cannot return arrays from functions in C++. You can however return a type that uses the std::vector or std::array template. Commented Mar 6, 2015 at 22:27
  • Instead of returning an array, pass the array in as a pointer and write to that pointer/array. Commented Mar 6, 2015 at 22:33
  • I want to create an array1 of char as pointerView in function, and create a lot of this arrays, and then add this "arrays1" to another array. I know like to do in swift and obj c, but here i don't uderstand. can you give some code? Commented Mar 6, 2015 at 22:39
  • 3
    Please edit your question to explain what you want to do, and include the code you tried that didn't work. Commented Mar 6, 2015 at 22:50

1 Answer 1

1

As you mentioned that you are not very strong with c++, and your question is rather unclear, here are several suggestions.

To get access to a class's attributes, c++ has the notion of visibility; The default visibility is private, that is, attributes and functions will not be visible outside of the class:

class Foo {
    int some_value;
};

There are several ways you can retrieve data from an object, however to put it simply, you should either make the attribute public:

class Foo {
public:
    int some_value;
};

or expose it via accessors/mutators:

class Foo {
    int some_value;
public:
    int get_some_value() { return some_value; }
    void set_some_value(int v) { some_value = v; }
};

Another thing to note is that you can not return arrays! In c++, when an array passes a function boundary (that is to say, passed as a parameter to, or returned from), and in a lot of other cases, an array will 'decay' in to a pointer. For example, the following is how I would pass an array of characters (otherwise known as a c-string) to a function:

#include <iostream>

using namespace std;

void print_cstr(const char *cstr) {
    cout << cstr << endl;
}

int main() {
    const char my_cstr[20] = "foo bar baz qux";
    print_cstr(my_cstr);

    return 0;
}

So what happens for N-dimensional arrays? Well, if char[1] decays to char*, then char[1][1] will decay to char**, and so on. You might have noticed this with the older main signature in C programs, which is used to pass an array of strings representing arguments passed to the program:

int main(int argc, char **argv) { ... }

It is very important that you realise that this really is no longer an array. C style strings are a bit special, in that they are conventionally terminated with a null byte \0, which means that you can usually tell where the end of the string is, or how long it is. However, you no longer have any information on how long the array is! For example, this is completely legal:

#include <iostream>

using namespace std;

void bad_fn(const int *nums) {
    for (unsigned i = 0; i < 20; ++i) {
        cout << "num " << i << " = " << nums[i] << endl;
    }
}

int main() {
    const int my_nums[5] = { 1, 2, 3, 4, 5, };
    bad_fn(my_nums);

    return 0;
}

Your function will end up reading memory beyond the bounds of the array, as it has no way of knowing where the array begins or ends (after all, array indexes are just pointer arithmetic). If you do not want to have to worry about keeping track of, and passing around the length of your array (and I would suggest that you do not!), please look at using one of the C++ standard library's containers. std::vector and std::array are two examples that would fit in the use case you have provided, and you can find decent documentation for them here.

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

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.