1

I try to create a class that accept and return an array but I got some problem. I'm not sure if it is legal to return an array from a class. Or it could be done by returning an pointer to the array. Thank for any solution to the problem.

#include <iostream>
using namespace std;

class myclass {
private:
    int Array[10];

public:
    myclass (int temp[10]) {
        for (int i = 0; i < 10; i++) {
            Array [i] = temp [i];
        }
    }


    int returnArray () {
        return Array; // error here, I'm not sure if it is legal to return an array.
    }

    int* returnArray2 () {
        return this->Array; // hope it will return a pointer to the array
    }
};


int main () {
    int Array[10] = {1,2,3,4,5,6,7,8,9};
    myclass A(Array);
    cout << A.returnArray() << endl; // try to return an array and print it.

    myclass* ptr = &A;
    cout << *ptr->returnArray2 << endl; // error here
    return 0;
}
goofy
  • 15
  • 5

3 Answers3

2

First of all it is better to write the constructor either like

myclass ( const int ( &temp )[10] ) {
    for (size_t i = 0; i < 10; i++) {
        Array [i] = temp [i];
    }
}

or like

myclass ( int temp[], size_t n ) : Array {} {

    if ( n > 10 ) n = 10;
    for (size_t i = 0; i < n; i++) {
        Array [i] = temp [i];
    }
}

Or even you may define the both constructors.

As for the returning value then you may not return an array. You may return either a reference to an array or a pointer to the entire array or a pointer to its first element

For example

int ( &returnArray () )[10] {
    return Array; 
}

In this case you can write in main

for ( int x : A.returnArray() ) std::cout << x << ' ';
std::cout << std::endl;

As for this statement

cout << *ptr->returnArray2 << endl; // error here

then you forgot to place parentheses after returnArray2. Write

cout << *ptr->returnArray2() << endl;

And the following member function is wrong because the expression in the return statement has type int * while the return type of the function is int

int returnArray () {
    return Array; // error here, I'm not sure if it is legal to return an array.
}

So either the function will coincide with the the second member function if you specify its return type like int *. Or you could change the return expression to *Array

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
int returnArray () {
    return Array; // error here, I'm not sure if it is legal to return an array.
}

This is illegal because Array is not of int type. Your returnArray2 is valid, however. As for this line:

cout << *ptr->returnArray2 << endl; // error here

This is illegal because returnArray2 is a function; you must call it to return the int*:

cout << *ptr->returnArray2() << endl; // prints the first value in the array

Other notes:

  • Your capitalization is backwards; you should call your class MyClass and your member array arr or arr_, or you will confuse a lot of people.
  • return this->Array; this is redundant, you can simply return Array;
  • If you haven't heard of std::vector and std::array you should research those, as they are generally superior to C-style arrays.
jcai
  • 3,448
  • 3
  • 21
  • 36
0

In general, I would suggest to read a c++ book to get your basics correct as there are lot of issues in the code you posted.

Regarding your main question about exposing C style arrays in class public API, this is not a very robust mechanism. Do it if it is absolutely essential because of existing code but if possible prefer to use std::vector. You will mostly always end up with better code.

Other answers have corrected your coding errors, so i won't repeat that.

One other thing, your code suggests that the array size is fixed. You can pass and return the array by reference as well. Refer to: General rules of passing/returning reference of array (not pointer) to/from a function?

Community
  • 1
  • 1
Paani
  • 560
  • 3
  • 11