How do I call a custom value constructor for a member variable?

I have a class that looks something like this:

template<int SIZE>
class MyClass{
public:
    MyClass(int a, int b){}
}

and I want an instance of MyClass in another class:

class X{
    MyClass<10>??   // How do I pass values to constructor args a and b?
}

but I'm not sure how to pass arguments to a constructor with two arguments when declaring an object as a member variable?

+4
source share
1 answer

If you are using C ++ 11 or later, you can write

class X{
    MyClass<10> mcTen = {1, 5};
}

Demo 1.

Prior to C ++ 11, you would need to do this in the list of constructor initializers:

class X{
    MyClass<10> mcTen;
    X() : mcTen(1, 5) {
    }
}

Demo 2.

+8
source

All Articles