Input dynamic array in OOP

How can input some dynamic array in my class(2ns line of code in main())? Is void inputArray optimaly written?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;


class myArray
{
private:
	int *m_array;
	int m_length;

public:
	myArray(int length)
		: m_length(length)
	{
		m_array = new int[length];
		m_length = length;
	}

	~myArray()
	{
		delete[] m_array;
	}

	int& getIndex(int index) { return m_array[index]; }
	void fillArray(vector<int> numbers)
	{
		for (int index = 0; index < m_length; ++index)
		{
			m_array[index] = numbers[index];
		}
	}
};

int main()
{
	myArray array(10);
	array.fillArray(); //how to input (from console or getting in from database)dynamic array?

	return 0;
}
Last edited on
Is void inputArray optimaly written?

Given that it doesn't exist, hard to critique it.

I assume this is an exercise you're doing? If you actually needed to do this, you'd just use a vector. I see your fillArray function takes in a vector, but does nothing with it. What's that about?

1
2
3
4
5
6
7
8
9
10
	void fillArray(vector<int> numbers)
	{
		for (int index = 0; index < m_length; ++index)
		{
                        int value;
                        cout << "Enter value: ";
                        cin >> value;
			m_array[index] = value;
		}
	}
Sorry, yes it's fillArray function that I want to know if it can be written better. Well, the idea was that I will dynamically allocate memory with std::vector...with length and it's values being known from input. That "cout << "enter value:" is what I was looking for and I am just readng about overloading [] so thanks!
May I ask you some more details? Just to avoid writing code you don’t need.
What are you trying to achieve? If you want to do your version of std::vector, well, I think you shouldn’t use std::vector at all :-) std::vectors already do what you seem to be asking for, so, if you can use them, just use them.
If that’s not what you’re trying to achieve, could you please clarify what your class should do?
And, please, could you answer Repeater’s question and tell us if this is a teacher’s assignment?
Yes it was assigment, but I wanted to include dynamicaly allocated array, instead of just plain string etc. . But now I did some self study and found std::list_initializer among other things, so thanks!
But now I did some self study and found std::list_initializer among other things, so thanks!

Does it mean you solved your issues and don’t need further help? If so, you consider ticking this post as solved. Thanks.
Topic archived. No new replies allowed.