Overwritten virtual does not get called as expected

1
2
3
4
5
6
7
8
9
10
11
class A{
public:
virtual void func();
};

class B : public A{
void func(bool f = true);
};

A* var = new B();
var->func(); // This should call B's func(), but calls the one from A 

Why is this happening? The default parameter isn't considered as a separated signature (func())?
the signatures don't match, you are not overriding it.
http://en.cppreference.com/w/cpp/language/override
The default parameter isn't considered as a separated signature (func())?

Parameters aren't signatures, so your question doesn't make much sense.

But to addresss what I think you're asking, B::func is a function which takes as an argument one value of type bool. A::func is a function which takes no arguments. Providing a default argument for B::func does not in any way change the signature of the function. So, as ne555 says, the signatures do not match.

If you use the override keyword where you think you are overriding, the compiler will tell you if you aren't.

1
2
3
class B : public A{
void func(bool f = true) override ;   // <--- error, function doesn't override
};


Good Luck !!
You say that I'm not overriding...
But if I call just func() (with no args, and let's say I don't have the default argument in B), then my compiler says that there isn't such function and I need to add "using A::func" to make it work... how is this "behaviour" called?
Last edited on
Topic archived. No new replies allowed.