3

In read in this post that you could override a virtual function with other default arguments, but that it is not smart to do. My case is slightly different from the question there. The function in my base class and the function in my derived class should do exactly the same only the default argument is different. How should I go about achieving this?

EDIT with some sort of code example what I think might work but violates what is said in the earlier mentioned post and doesn't seem very neat to me:

class base {
public:
    //not sure about the virtual here
    virtual void func(bool something = true) { /*Do some stuff*/};
};

class derived : public base {
public:
    void func(bool something = false) override
    {
        base::func(something); /*Doesn't seem like the way to go for me */
    }
};
7
  • You need to post a code example. Otherwise, it's hard to get what you're trying to say.
    – L. F.
    Commented Apr 24, 2020 at 10:09
  • @L.F. I added some code
    – C. Binair
    Commented Apr 24, 2020 at 10:19
  • The linked question applies as well. You will end up with something = true if you call func from a base* pointer.
    – L. F.
    Commented Apr 24, 2020 at 10:21
  • @L.F. Yes I understood that from the linked question, but what should you do then to prevent this? BTW is the calling to ```base::func()```` in the func definition in the derived class how you would do this? To me that seems a bit of a stupid way.
    – C. Binair
    Commented Apr 24, 2020 at 10:23
  • You have to provide two virtual functions then. What is the use case for this though?
    – L. F.
    Commented Apr 24, 2020 at 10:25

2 Answers 2

5

One option is to explicitly overload the function, and not rely on default arguments

 class Base
 {
     public:
         virtual void func() {func(2);}   // call func(int) with one default
         void func(int value);
 };

 class Derived: public Base
 {
      public:
         using Base::func;    // so the function which accepts an argument is
                              //   not hidden from users the class
         virtual void func() {func(42);}    // call Base::func(int) with different value
 };

Depending on need, it may be easier for the default value to be a member that is set by the constructor.

 class Base
 {
     public:
         Base() : default_value(2) {};
         void func() {func(default_value);}
         void func(int value);

     protected:
         Base(int defaulted) : default_value(defaulted) {};

     private:
         int default_value;
 };

 class Derived: public Base
 {
      public:
         Derived() : Base(42) {};    // set different default value in the constructor
 };

Depending on your need (and given that the requirement is only that the function be supplied with different defaults, but are otherwise the same) you may wish to consider leaving the functions non-virtual.

4
  • Is it necessary in your first code example that the func in the derived class is virtual as well? I'm quite sure that my Derived class will not be the base class for another class
    – C. Binair
    Commented Apr 24, 2020 at 10:38
  • In the first version, it would be necessary for the func() without arguments to be virtual if you use the base polymorphically, since you would essentially be overriding it to change how it calls func(int). You've only stated that the requirement is to have different default values - for that it is not necessary that func(int) be virtual, but (depending on other requirements of your classes) it can be. If you make do make both func() and func(int) virtual, think through usage scenarios and then test carefully to ensure all use cases work as you intend if you override both.
    – Peter
    Commented Apr 24, 2020 at 11:12
  • So I'm I correct to say that if a function in a base class is virtual, the same function in the derived class should also be virtual? Like in general, always?
    – C. Binair
    Commented Apr 24, 2020 at 11:17
  • 1
    If a function in the base class is virtual, any override of it (with same type of all arguments) in a derived class is virtual, whether you explicitly specify that or not. Overriding a virtual function does not make it non-virtual (although, C++11 and later, you can use the final specifier to prevent further derived classes overriding it).
    – Peter
    Commented Apr 24, 2020 at 11:25
2

You might have overload

class base {
public:
    virtual ~base() = default;
    virtual void func() { func(true); };
    virtual void func(bool something) // Maybe this one no longer need to be virtual
    { /*Do some stuff*/ }             // then using different name is appropriate
                                      // (avoid hiding issues)
};

class derived : public base {
public:
    using base::func;

    void func() override { func(false); }
};
2
  • Is there a specific reason why you use the keyword override in the derived class? I thought it was almost never really necessary
    – C. Binair
    Commented Apr 24, 2020 at 10:39
  • 1
    Not necessary but helpful. It allows compiler to warn you about mistake. void func(int) override would produce error.
    – Jarod42
    Commented Apr 24, 2020 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.