Click to See Complete Forum and Search --> : Problem passing arrays to constructors


smokey1401
July 13th, 2008, 06:19 PM
I have a class whereby one if its members is an array of objects from another class (not derived). When I construct this class, I want to pass an array of these objects to it, whereby each object of this class instantly has an array of the [other] objects which it can later access. How do I go about writing the constructors? Here is what I have tried:

Class ThisClass
{
private:
enum { MAX = 10 };
OtherClass m_array[MAX];
int m_one;
int m_two;
public:
ThisClass() : int m_one(0), int m_two(0) , m_array (WHAT GOES HERE) {}
ThisClass(int one, int two, int array[]) : m_one(one), m_two(two), m_array(array) {}

This does not compile. I am confused when I should include the square brackets after the array name. When I experiment with and without them I get different errors.

To clarify: When I create an object of the above class I would like to pass it an array of objects of another class. I am yet to find a solution on the net.

A common compile error I am getting is "cannot specify explicit initializer for arrays"

Hope you can help. Thanks

Paul McKenzie
July 13th, 2008, 06:33 PM
Arrays are not copyable.

int main()
{
int a[10];
int b[10];
a = b; // error
}
That is essentially what your code is trying to do.

First, this definition:

ThisClass(int one, int two, int array[]);

is no different than this one:

ThisClass(int one, int two, int* array);

The syntax using [] doesn't change the fact that you are passing the pointer to the first element of the array. Therefore you are never really passing an entire array, only a pointer to the first element.

If you want to really pass an array:

1) Don't use arrays and use std::vector<int> which is safer, or

2) Wrap the array in a struct and pass the struct, since structs are copyable:

struct ArrayStruct
{
int array[10];
};
//...
ThisClass(int one, int two, ArrayStruct& array);

Then in the constructor body, you have to set each element of your member array to the array in ArrayStruct.

To clarify: When I create an object of the above class I would like to pass it an array of objects of another class. I am yet to find a solution on the net.1) Use std::vector<YourObject> instead of arrays

2) Do not exclusively use the net to learn C++. Use proper C++ books. The reason is that there is a lot of bad and wrong information concerning C++ program at various websites. If you want the net to get a proper introduction to C++:

http://www.parashift.com/c++-faq-lite/

C++ isn't conducive to "experimentation" and "seeing what sticks" type of programming. You need to get proper books and tutorials on the language.

Regards,

Paul McKenzie

smokey1401
July 13th, 2008, 06:51 PM
Thanks for your quick reply. I have not got a chance to analyze your response right now, but I shall soon and give you my feedback.

With regards to learning c++, I completely agree with textbooks. I am reading Object-Oriented Programmig in C++ (very good) but have not found a solution. I have ordered "C++ Primer 5th Edition" so maybe this will help clarify my problem further.

Regards

Lindley
July 13th, 2008, 09:10 PM
You're dealing with two fundamentally different views of arrays here. The one you declared inside the class lives in there entirely; in other words, sizeof(m_array) == sizeof(OtherClass)*MAX.

However, what you're passing to the constructor is just a pointer, not an entire array, as Paul says. In other words, sizeof(array) == 4.

An analogy would be, the first is a librarian staggering around carrying a huge stack of books, while the second is a librarian pulling a cart containing a stack of books. In both cases the librarian can get at the books, but the actual thing he's holding in his hand is different.

Clearly, you can't just set one equal to the other. You thus have two options.

1) If you want the class member m_array to always refer to the same *actual* array that you pass in----so that the object's view of that array changes when anyone else changes that array in the future----then declare m_array as an OtherClass*. You can then simply assign m_array = array.

2) If you'd like m_array to contain a snapshot of the array you pass in at that time but to behave as a separate array in the future completely owned by the containing object, then either use a std::vector, or do something like:

for (i = 0; i < MAX; i++)
m_array[i] = array[i];

inside the constructor.

smokey1401
July 14th, 2008, 11:18 AM
Thanks Lindley, much appreciate the library analogy ha! The second option looks good, I shall get to work and report any problems.

Thanks again to both yourself and Paul