how to declare array in Constructor

Never met this task before, but how to declare array in Constructor?
Actually, you need to declare your "array" as a data member of the class. You might initialise and possibly resize it in a constructor.

Depends a bit on what type of container you want to use as an "array" (indexed variable) - C-style arrays or dynamic arrays; std::array<T>, std:vector<T>, ...

You need to give more information.

Probably I need to call it initialize with array subcripts :)

Test::Test(int id_value, const string &name_value): id( id_value){
setName (name_value);
}

I want to have setter and getter function to assign values inside constructor
Do you mean for a c-style array like this eg:

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
#include <algorithm>
#include <iostream>
#include <iterator>

constexpr size_t noElems { 6 };

struct S {
	S(const int a_[noElems]) {
		std::copy_n(a_, noElems, a);
	}

	void display() const {
		std::copy(a, a + noElems, std::ostream_iterator<int>(std::cout, " "));
		std::cout << '\n';
	}

	int a[noElems];
};

int main() {
	const int b[noElems] { 1,2,3,4,5,6 };
	S s { b };

	s.display();
}


or for std::array or for std::vector?
Last edited on
@seeplus,

Is there a reason you didn't use NoElems in line 17 when you declared a?
No. My mistake. Corrected...
or possibly something like for C++20:

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
#include <algorithm>
#include <iostream>
#include <iterator>
#include <span>
#include <vector>

struct S {
	S(std::span<const int> a_) {
		std::copy(std::begin(a_), std::end(a_), std::back_inserter(a));
	}

	void display() const {
		std::copy(a.begin(), a.end(), std::ostream_iterator<int>(std::cout, " "));
		std::cout << '\n';
	}

	std::vector<int> a;
};

int main() {
	const int b[] { 1,2,3,4,5,6 };
	S s { b };

	s.display();
}

Last edited on
I want to have setter and getter function to assign values inside constructor


You can't return a c-style array from a getter-function. For a setter-function this is similar to the constructor. For a getter, you could return a std::vector.

Have you an example of what you're trying to do with 'array'?
Last edited on
I want to have setter and getter function to assign values inside constructor

A class typically has private members (implementation) and public interface.

In code above:
1
2
3
4
5
int main() {
	const int b[] { 1,2,3,4,5,6 };
	S s { b };
	s.display();
}

The constructor and S::display() const are the interface, how other code can interact/use objects of type S.

The implementation of all members of S (like the constructor) has access to all members of S (like the a). They do not need to use any getters/setters, but may do so in order to avoid code duplication. If you simply read/modify value of an element of the array, then there is no need for getter/setter in that.
Yes. I used a struct for an example of a constructor for simplicity as the OP didn't mention class. Of course, for a struct everything is public by default whereas with a class everything is private by default.
Topic archived. No new replies allowed.