Hi,

I have faced a problem that I have a vector in my class which I would like to initialize using constructor initialization list with some thing like this std::vector<double>({1,4,5}) which is only allowed in c++11 which is not what I am looking for. I could only intialize it with one value 0 as shown in the code below. Is there is anyway I can do it without going to the constructor body?

Data::Data()
    :V(std::vector<double>(3,0)),    
/* Default Constructor */
{
}

thanks!

Recommended Answers

All 8 Replies

Are you looking for a default constructor which will initialize the std::vector member with the values 1, 4, 5 via the constructor initialization list without embracing the C++11 features?

I know there isn't constructor initialization except in c++11. So I am asking is there is anyway to instialize the vector with these values "1, 4, 5" without going to the constructor body.
Maybe somehow using copy constructor and a temp variable, but can I do that?

Are you talking about something like this?

#include <iostream>
#include <vector>
#include <iterator>

template <class T>
class test
{

public:

    test(T a[], size_t s):vec(a, a + s ) {}

    void display_vec() const
    {
        std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(std::cout, " "));
    }

private:    

    std::vector<T> vec;

};

int main(int argc, char** argv)
{
    unsigned long mya[] = {1,4,5,7};
    test<unsigned long> you(mya, sizeof(mya)/sizeof(unsigned long));

    you.display_vec();

    return 0;
} 

No, I want to inistialize the vector from inside the constructor initialization list the goal is not to use the constructor body too, only from the list:

Data::Data()
:V(/* i want to assign the values 1 4 5 from here */),
/* Default Constructor */
{}

No, I want to inistialize the vector from inside the constructor initialization list the goal is not to use the constructor body too, only from the list

I thought I was doing that in my example.

No, what you did was creating an array in the main function and passed the values to the constructor. I want these values to be given to the vector from the constructor instialization list it self.
I want something equivelent to this in c++11:

    Data::Data()
    :V(std::vector<double>({1,4,5})
    /* Default Constructor */
    {
    }

The closest you can get is probably something like this:

class Data {
  // ....

  private:
    static const double* getInitValues() {
      static const double arr[] = {1.0,4.0,5.0};
      return arr;
    };

  public:

    Data() : V(std::vector<double>(getInitValues(), getInitValues() + 3)) 
    {
    };

};

But frankly, I don't see the point of going through all this trouble just to either (1) avoid using a C++11 feature, or (2) avoid putting three simple statements in the constructor body.

I can't use c++11. Using the constructor body is an option but I wanted to learn if it is possible.
thanks alot!

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.