Proper Initialization of member variables

Apr 10, 2023 at 3:19pm
Given a class "MyClass" defined as follows:

1
2
3
4
5
6
7
8
class Myclass
{
    public:
    Myclass();
    typedef vector<Myclass> vectorField;
    vector<double> value;
    int N, M;
}

Consider that I have created the following object:
1
2
//in main
  Myclass::vectorField Vec;


It seems that I have a problem with initialization. How to properly initialize the class?

The problem appears when I try to access the vector object for any of its member values like the following:
1
2
3
4
5
     double value;
     for (int i = 0; i < (10); i++)
     {
        value= Vec[i].M;
     }


The code compiles with GCC, but it terminates upon running. Could you please help me identify the problem and the correct way of initialization?

Apr 10, 2023 at 3:45pm
I'm not sure what you are trying to achieve. Perhaps you could give some more details. However to initialise a struct/class and display some values consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

class Myclass {
public:
	std::vector<double> value;
	int N {}, M {};
};

using vectorField = std::vector<Myclass>;


int main() {
	const vectorField Vec { {{1, 2, 3}, 2, 3 }, {{ 2, 3, 4 }, 3, 4} };

	for (const auto& v : Vec) {
		for (const auto& va : v.value)
			std::cout << va << ' ';

		std::cout << '\n';
	}
}


which displays:


1 2 3
2 3 4

Apr 10, 2023 at 7:41pm
This creates an empty vector (containing zero elements).
 
Myclass::vectorField Vec;

This creates a vector containing 10 elements.
 
Myclass::vectorField Vec(10);

This adds one element to the end of the vector.
 
Vec.push_back(Myclass());


When accessing vector elements using [] you need to be sure the vector is big enough. Otherwise the behaviour is undefined.
Last edited on Apr 10, 2023 at 7:42pm
Apr 11, 2023 at 10:30am
Also consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

class Myclass {
public:
	using vectorField = std::vector<Myclass>;

	std::vector<double> value;
	int N {}, M {};
};


int main() {
	Myclass::vectorField Vec { {{1, 2, 3}, 2, 3 }, {{ 2, 3, 4 }, 3, 4} };

	Vec.emplace_back( std::vector<double>{ 5, 6, 7 }, 4, 5 );

	for (size_t i {}; i < Vec.size(); ++i)
		std::cout << Vec[i].M << ' ';

	std::cout << '\n';
}


when using [] with a vector, the specified element number must be >= 0 and < .size().
Apr 12, 2023 at 7:25am
Consider that I have created the following object:
1
2
//in main
Myclass::vectorField Vec;

That is equivalent to:
1
2
//in main
std::vector<Myclass> Vec;

Just like std::vector<int> integers;, the Vec is an empty vector that contains no elements.

The std::vector does have constructors that help you initialize the vector in multiple ways.
Some of them create elements and assign values to those elements, like above:
1
2
3
4
Myclass::vectorField Vec {
      {{1, 2, 3}, 2, 3},
      {{2, 3, 4}, 3, 4}
   };

That construction supplies values for two Myclass elements.

The only difference between std::vector<int> and std::vector<Myclass> is the type of element.


If you would create just one Myclass object: Myclass obj ;,
then how would you initialize that?
Topic archived. No new replies allowed.