Asmita Poddar
Asmita Poddar

Reputation: 532

In-class constructor initialisation of a vector in C++

I have class MyClass as below. I would like to initilaise the attributes of MyClass through a constructor. The arguments b_ and c_ should have the defaults {"abc", "_"} and 0 respectively.

class MyClass {

    size_t a_;
    std::vector<string> b_;
    size_t c_;

public:

    MyClass(
            size_t a,
            optional<std::vector<string>> b = {"abc", "_"},
            size_t c = 0)
        :   a_(a),
            b_(b),
            c_(c)
        {}
}

This gives me the error: error: could not convert ‘{"abc", "_"}’ from ‘<brace-enclosed initializer list>’ to ‘isf::optional<std::vector<std::__cxx11::basic_string<char> > >’.

I then tried:

class MyClass {

    size_t a_;
    std::vector<string> b_;
    size_t c_;

public:
    const std::vector<string> VEC = {"abc", "_"};

    MyClass(
            size_t a,
            optional<std::vector<string>> b = VEC,
            size_t c = 0)
        :   a_(a),
            b_(b),
            c_(c)
        {}
}

This gave me the error: error: invalid use of non-static data member ‘MyClass::VEC’.

Making VEC static also did not solve the issue: error: in-class initialization of static data member ‘const std::vector<std::__cxx11::basic_string<char> > MyClass::b’ of non-literal type

What would be the correct way of declaring a default value for the vector of strings in the constructor?

Upvotes: 0

Views: 63

Answers (2)

Asmita Poddar
Asmita Poddar

Reputation: 532

It can be solved in the following way:

class MyClass {

    size_t a_;
    std::vector<string> b_;
    size_t c_;

public:
    const std::vector<string> DEFAULT_VEC = {"abc", "_"};

    MyClass(
            size_t a,
            optional<std::vector<string>> b,
            size_t c = 0)
        :   a_(a),
            b_(b.value_or(DEFAULT_VEC)),
            c_(c)
        {}
}

Upvotes: 0

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122458

The right way is to use a std::vector<std::string>>:

MyClass(
            size_t a,
            std::vector<std::string> b = {"abc", "_"},
            size_t c = 0)
        :   a_(a),
            b_(b),
            c_(c)
        {}

There is no use of optional here, because either the caller does pass a vector or the caller does not pass a vector but then the default is used. As you want to initialize the member always, passing nullopt is not an option either. Hence, just drop the optional.

Upvotes: 2

Related Questions