I am spoiling the comma operator in the constructor, so that I can preprocess the parameters and use the processed parameter for initialization.
Say I have the following base and derived classes:
class Base
{
protected:
int i;
int j;
public:
Base(int a):i(a),j(a){}
Base(int a, int b):i(a),j(b){}
};
class Derived:public Base
{
private:
int d;
void inc(int & a) {a++;}
void inc(int & a, int & b) {a++; b++;}
public:
Derived(int a, int b, int c);
};
I know I can use comma operator to process a parameter and use it to initialize the base part as follows:
Derived::Derived(int a, int b, int c):Base((inc(a),a)),d(c){}
Now what about if I want to pre-process two parameters and use them to initialize the base? I did the following:
Derived::Derived(int a, int b, int c):Base((inc(a,b),(a,b))),d(c){}
But this is not what I want, because the single-parameter base constructor will still be used (since (a,b)
is also a comma operator that returns b
). Is there anyway to achieve what I want if comma operator is not possible?