• Advertisement
Sign in to follow this  

[C++] Array Question

This topic is 4251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, say I have a class with an array, and then in the constructor, it chooses how big the array is supposed to be, because it recieves the size as an argument, like so:
class myClass
{
     //I want the array to be in this scope so that other functions can use it...
     myClass(int size)
     {
         //but I need the size of the array to be here so that you can send the size as an argument.
     }
}

I just don't know how the syntax is supposed to be, like would I type "int array[]" outside of the constructor and "array[size]" inside or what?

Share this post


Link to post
Share on other sites
Advertisement


// With array

class myClass {
const int size;
Type* const array;

myClass(int size) : size (size), array (new Type[size]) { }
~myClass() { delete [] this->array; }
};

// With vector

#include <vector>

class myClass {

vector<Type> array;
myClass(int size) : array (size) { }
};


Share this post


Link to post
Share on other sites

class myClass {
// store the size of the array here
const int size;

// use a pointer to the first element of the array.
// the pointer is constant, so it cannot be made to
// point to something else.
Type* const array;

// constructor: the initializer list sets the values for
// the two constants.
myClass(int size) :

// The size, as given by the user
size (size),

// The array, allocated dynamically with the correct size.
array (new Type[size]) { }

// Don't forget to free the dynamic memory when the object
// is destroyed.
~myClass() { delete [] this->array; }
};

#include <vector>

class myClass {

// A vector of arbitrary size.
std::vector<Type> array;

// Initialize the vector with size elements. You may
// enlarge or shrink it later as needed.
myClass(int size) : array (size) { }

// Great: the vector is freed automatically, you don't
// have to free it by hand like with a dynamic array.
};


Share this post


Link to post
Share on other sites
Quote:
Original post by Grantyt3
Theres no way to do it without dynamically allocating memory?
If you want to be able to determine the size of the array at run time, then the array will have to be dynamically allocated, in which case std::vector<> is your best bet.

Depending on what it is exactly that you're doing though, there may be other solutions. For example, if you wanted arrays of different (but fixed) sizes, you could use a class template instead. Anyway, if you'd like more specific suggestions maybe you could tell us what the class actually is, and what sort of information the array holds.

Share this post


Link to post
Share on other sites
Quote:
Original post by Grantyt3
Okay, say I have a class with an array, and then in the constructor, it chooses how big the array is supposed to be...


Quote:
Original post by Grantyt3
Theres no way to do it without dynamically allocating memory?

I believe that's what you asked how to do.

Share this post


Link to post
Share on other sites
Any time something is created in C++, its size must be known. Additionally, the size of individual data types (as opposed to arrays thereof) must be calculable from the class definition. Object layouts can't be variable-sized things, because then how would you create an array of the objects (among other problems)?

By doing dynamic allocation, the class just holds a pointer-to-X, which is the same as a pointer-to-dynamic-array-of-X. The number of X's isn't known, but the size of an X is known, and the size of *the pointer* is known, which is enough.

Seriously, though, just use std::vector. It wraps this process for you, and offers several nice extra bits with a minimum of overhead:

- It can be resized as needed.
- It "remembers" its length, which you will probably need to know at run-time for any given class instance. (What good is it for other functions to use the array if they don't know how many elements are in it?)
- It won't require you to write memory management code in a destructor, copy constructor or assignment operator (both of which Toohr overlooked); you can treat the vector instance as if it were an "ordinary" data member.
- You can just "add stuff on to the end" with .push_back, which will automatically resize, in an intelligent fashion.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement