I have a problem about std::vector as member of class, I'm not clearly about the initialization mechanism

#include <iostream>
#include <vector>
using namespace std;

class Obj; //this is a Class object
class Grid
{
public:
	Grid(  ) { }
	~Grid(  ){ }
protected:
private:
    vector<Obj> v1; //initialization mechanism ? and specific requirement about Obj?
	vector<int>  v2; //does it need to be initialized in constructor function?
	//more difficult case
	vector<Obj*> v3;
};

Recommended Answers

All 7 Replies

No problems with v1 and v2: these members constructors create empty vectors. You can initialize them in Grid constructor(s) or later in the special member function (let it's named as init or what else), for example:

explicit Grid::Grid(size_t n): v2(n), v3(n) {}
// or
void Grid::initv2(const std::vector<int>& v) { v2 = v; }
void Grid::initv2(const int* a, size_t n)
{
    v2.clear();
    if (n > 0) {
        v2.resize(n);
        for (size_t i = 0; i < n; i++)
            v2[i] = a[i];
}

However you can't declare std::vector<Obj> v1 from incomplete Obj declaration class Obj; : template std::vector wants complete element type definition.

Firstly what do you mean by initialisation mechanism.

If you are trying to pre initialise your vector. The best way to do it is through a constructor.

Check this site out
http://www.cplusplus.com/reference/stl/vector/vector/

I just mean, in my case, v1, v2 should be initialized in the constructor function or not. and how vector<Obj> v3 is initialized, we suppose Obj is a defined object and has its own ctor function(constructor function).

>>I just mean, in my case, v1, v2 should be initialized in the constructor function or
>>no
Do don't need to construct vectors explicitly. When you define a vector a type T, the vector's constructor automatically calls the constructor of T for all the elements.
This poses a restriction that T should have at least one constructor.

No problems with v1 and v2: these members constructors create empty vectors. You can initialize them in Grid constructor(s) or later in the special member function (let it's named as init or what else), for example:

explicit Grid::Grid(size_t n): v2(n), v3(n) {}
// or
void Grid::initv2(const std::vector<int>& v) { v2 = v; }
void Grid::initv2(const int* a, size_t n)
{
    v2.clear();
    if (n > 0) {
        v2.resize(n);
        for (size_t i = 0; i < n; i++)
            v2[i] = a[i];
}

However you can't declare std::vector<Obj> v1 from incomplete Obj declaration class Obj; : template std::vector wants complete element type definition.

thx to ArkM, I now konw v1 v2 can be not initialized in the constructor function, that is to say, not necessary to do it. So I can add data in v1 or v2 directly in the later process, Do you think my understanding is right? by the way, class Obj is a defined class in my code, and it owns constructors.

More precisely: v1 and v2 are initialized if both are well constructed empty vectors. The class vector default constructor (constructor without parameters) constructs an empty vector.
The C++ Standard:

A constructor is used to initialize objects of its class type.

Yet another example:

void Grid::Grid(const vector<int>& vi): v2(vi) {}

There is so called ctor-initializer list in this Grid constructor: v2 member now is directly-initialized by the value of this constructor parameter. It's a preferred form of a member initialization if there is a proper non-default constructor of this member class.

Thank you for all your help, I think this gives me more understanding about object construction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.