Overwritten virtual does not get called as expected

Mar 19, 2016 at 1:45pm
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())?
Mar 19, 2016 at 10:39pm
the signatures don't match, you are not overriding it.
http://en.cppreference.com/w/cpp/language/override
Mar 20, 2016 at 1:08am
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.

Mar 20, 2016 at 1:51am
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 !!
Mar 22, 2016 at 7:13pm
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 Mar 22, 2016 at 7:14pm
Mar 22, 2016 at 11:09pm
Topic archived. No new replies allowed.