Class member variable array

Since everyone here has been so helpful, I have another question for forum gurus:

Suppose I wish to declare an object with a public function, void someFunction(), that will generate grades for a student. You need an integer array to store these grades (0~100).

I understand that I can create a dynamic array within the brackets of the function and declare a pointer in the class that can be used to access these values. But since the function returns void, won't these values be lost due to scope? If so then how to store it? (Note: I'm limiting myself to arrays only - not vectors or other storage types)

When a class is declared, is it possible to declare a variable array if the size is not determined at compile time? I was under the impression the size had to be known although I could be wrong. For example, is the following the proper approach?

#include <iostream>

class Foo
{

public:

Foo(int);
~Foo();

private:

int m_size;
int m_someArray[m_Size];

};

Foo:Foo(int arraySize = 1)
:m_size(arraySize)
{
}

Foo::~Foo () {}

int main() {


int size;
std::cin >> size; // user determines size of array at runtime

Foo foo(size); // create object & pass array size

return 0;
}

As always, thanks in advance.

L


Last edited on
An array size must be a constant. So, the example you showed above won't work. You need to use Dynamically Allocated Arrays to work around this issue.
Last edited on
Topic archived. No new replies allowed.