سؤال

Can I use the post-increment operator with initialization list arguments? For example something like this:

template <class T> struct MyClass
{
  MyClass(T& val): myvar(val++) {/*body*/}
  protected: T myvar;
};

And will code in the constructor body be affected by the change?

هل كانت مفيدة؟

المحلول

Yes you can. The myvar member will have the value of val before the increment.

We should considere that val will have a type supporting the post-increment operator.

template <class T> struct MyClass
{
    MyClass(T val): myvar(val++) {/*body*/}
//          ^
protected:
    T myvar;
};

MyClass<int> foo( 1 );

// Here myvar will have the value 1

And don't forget the type of val passed to the MyClass constructor.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top