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;
}
1
  • What are you going to do with the array? What is your goal? Commented Jul 29, 2015 at 13:20

3 Answers 3

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

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

2 Comments

Thank you for your solution and it is what I need. The syntax "int ( &returnArray () )[10] {} "is new to me because I started to learn C++ for only few weeks.
@goofy Beginners should help each other.:)
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.

3 Comments

Your first "note" is highly subjective. Though I use the same convention that you do, please don't posit it as an absolute!
I suppose so...although it's confusing when SE colors Array as a typename!
Only if that's not your style!
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?

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.