morgancodes
morgancodes

Reputation: 25265

C++ change default argument to constructor in derived class

Consider the class Mixer:

class Mixer{

    int numChannels;

    public: Mixer(int numChannels = 2):numChannels(numChannels){

    }

}

I have a subclass called Adder. I'd like the default of numChannels in adder to be 1. How can I achieve this?

Upvotes: 0

Views: 1078

Answers (3)

djechlin
djechlin

Reputation: 60768

Don't use default arguments - provide a default constructor that takes no inputs and sets it to the default you want.

Upvotes: 0

Peter
Peter

Reputation: 7324

Change the default value in the constructor of Adder:

class Adder: public Mixer {
  public:
    Adder(int numChannels = 1): Mixer(numChannels) {}
};

Upvotes: 4

K-ballo
K-ballo

Reputation: 81349

Something like this:

class Adder : Mixer
{
public:
    Adder(int numChannels = 1) : Mixer(numChannels){}
};

Upvotes: 5

Related Questions