tags:

views:

140

answers:

4

If I declare a base class (or interface class) and specify a default value for one or more of its parameters, do the derived classes have to specify the same defaults and if not, which defaults will manifest in the derived classes?

Addendum: I'm also interested in how this may be handled across different compilers and any input on "recommended" practice in this scenario.

+3  A: 

This is one that you can probably figure out reasonably well by testing (i.e., it's a sufficiently mainstream part of the language that most compilers almost certainly get it right and unless you see differences between compilers, their output can be considered pretty well authoritative).

#include <iostream>

struct base { 
    virtual void x(int a=0) { std::cout << a; }
    virtual ~base() {}
};

struct derived1 : base { 
    void x(int a) { std:: cout << a; }
};

struct derived2 : base { 
    void x(int a = 1) { std::cout << a; }
};

int main() { 
    base *b[3];
    b[0] = new base;
    b[1] = new derived1;
    b[2] = new derived2;

    for (int i=0; i<3; i++) {
        b[i]->x();
        delete b[i];
    }

    derived1 d;
    // d.x();       // won't compile.
    derived2 d2;
    d2.x();
    return 0;
}
Jerry Coffin
Look at all those leaks. :P
GMan
@GMan: [Carefully looking innocent] What leaks? :-)
Jerry Coffin
I think he's referring to the lack of a virtual destructor. But in this case it won't leak.
John Dibling
@Jerry, destructor has be virtual if you are deleting derived object though base class pointer. Otherwise base class destructor will be called for all of them. In this it is ok as there is no destructor. :-)
chappar
@John: Originally there were no deletes, which is what I was referring to. I totally ignored the lack of a virtual destructor. And... @chappar: No, it's not okay. It *must* have a virtual destructor to be deleted through a base class, or you get undefined behavior. (This code has undefined behavior.) It has nothing to do with what data or destructors the derived classes have.
GMan
@Chappar: The code originally didn't delete anything. Though it's mostly irrelevant to the question at hand, I've also added a virtual dtor to the base class -- with a trivial dtor, it rarely matters, but GMan is entirely correct that without it, the code has UB.
Jerry Coffin
+9  A: 

Virtuals may have defaults. The defaults in the base class are not inherited by derived classes. Some compilers may do something different, but this is what the standard says:

8.3.6.10:

A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides. [Example:

struct A {
  virtual void f(int a = 7);
};
struct B : public A {
  void f(int a);
};
void m()
{
  B* pb = new B;
  A* pa = pb;
  pa->f(); //OK, calls pa->B::f(7)
  pb->f(); //error: wrong number of arguments for B::f()
}
—end example]
John Dibling
Thanks for the reference, that tells me the behaviour I can reasonably expect across compilers (I hope).
Arnold Spence
+1 For standards reference
Mark B
+1 it is a good hint to not rely on compilers :)
PC2st
This is a correction to my previous summary: I will accept this answer for the reference and mention that the collective recommendation is that it is ok to have default parameters in virtual functions as long as they don't change default parameters previously specified in an ancestor class.
Arnold Spence
+3  A: 

This was the topic of one of Herb Sutter's early Guru of the Week posts. I haven't indepen

The first thing he says on the subject is DON'T DO THAT.

In more detail, yes, you can specify different default parameters. They won't work the same way as the virtual functions. A virtual function is called on the dynamic type of the object, while the default parameter values are based on the static type.

Given

class A {
    virtual void foo(int i = 1) { cout << "A::foo" << i << endl; }
};
class B: public A {
    virtual void foo(int i = 2) { cout << "B::foo" << i << endl; }
};
void test() {
A a;
B b;
A* ap = &b;
a.foo();
b.foo();
ap->foo();
}

you should get A::foo1 B::foo2 B::foo1

David Thornley
Thanks. A "Don't do that" from Herb Sutter carries some weight.
Arnold Spence
A: 

As you can see from the other answers this is a complicated subject. Instead of trying to do this or understand what it does (if you have to ask now, the maintainer will have to ask or look it up a year from now).

Instead, create a public non-virtual function in the base class with default parameters. Then it calls a private or protected virtual function that has no default parameters and is overridden in child classes as needed. Then you don't have to worry about the particulars of how it would work and the code is very obvious.

Mark B
It's not at all complicated. Default parameters are discovered along with name resolution. They follow the same rules.
Noah Roberts