Overloading Base Class Method in Derived Class

overloading base class method in derived class

Its called Name Hiding. When you define a non virtual method with the same name as Base method it hides the Base class method in Derived class so you are getting the error for

 myDerived.method_A(1,2);

To avoid hiding of Base class methods in Derived class use using keyword as you did in Derived2 class.

Also if you want to make it work you can do it explictly

myDerived.Base::method_A(1,2);

Check out this for better explanation why name hiding came into picture.

C++ Base Class Function Overloading in a Subclass

As you said, the BaseClass's function is hidden by the SubClass's. The name Func will be found at the SubClass's scope, then name lookup stops, Func in BaseClass won't be considered at all, even it's more appropriate. They're not "overloads" at all.

See Unqualified name lookup.

You can use using to introduce them into the same scope to make overloading works.

class SubClass : public BaseClass {
public:
using BaseClass::Func;
~~~~~~~~~~~~~~~~~~~~~
void Func(int i) { // accepts an int, not a float!
cout << "SubClass::Func() called!";
}
};

Overload a function with a derived class argument if you only have a pointer to the base class in C++

With double dispatch, it would be something like:

class Circle;
class Box;

// Overloaded functions (Defined elsewhere)
void ResolveCollision(Circle& a, Box& b);
void ResolveCollision(Circle& a, Circle& b);
void ResolveCollision(Box& a, Box& b);
class PhysicsObject // A pure virtual class
{
public:
virtual ~PhysicsObject() = default;

virtual void ResolveCollision(PhysicsObject&) = 0;
virtual void ResolveBoxCollision(Box&) = 0;
virtual void ResolveCircleCollision(Circle&) = 0;
};

class Circle : public PhysicsObject
{
public:
void ResolveCollision(PhysicsObject& other) override { return other.ResolveCircleCollision(*this); }
void ResolveBoxCollision(Box& box) override { ::ResolveCollision(*this, box);}
void ResolveCircleCollision(Circle& circle) override { ::ResolveCollision(*this, circle);}
// ...
};

class Box : public PhysicsObject
{
public:
void ResolveCollision(PhysicsObject& other) override { return other.ResolveBoxCollision(*this); }
void ResolveBoxCollision(Box& box) override { ::ResolveCollision(box, *this);}
void ResolveCircleCollision(Circle& circle) override { ::ResolveCollision(circle, *this);}
// ...
};

Overloading base method in derived class

To explain how it works for the OverriddenDerivedClass example:

Have a look at the C# spec for member lookup here: http://msdn.microsoft.com/en-us/library/aa691331%28VS.71%29.aspx

That defines how the lookup is done.

In particular, look at this part:

First, the set of all accessible (Section 3.5) members named N declared in T and the base types (Section 7.3.1) of T is constructed. Declarations that include an override modifier are excluded from the set.

In your case, N is Foo(). Because of Declarations that include an override modifier are excluded from the set then the override Foo(int i) is excluded from the set.

Therefore, only the non-overridden Foo(double i) remains, and thus it is the one that is called.

That is how it works for the OverriddenDerivedClass example, but this is not an explanation for the DerivedClass example.

To explain that, look at this part of the spec:

Next, members that are hidden by other members are removed from the set.

The Foo(double i) in DerivedClass is hiding the Foo(int i) from the base class, so it is removed from the set.

The tricky thing here is the part that says:

All methods with the same signature as M declared in a base type of S are removed from the set.

You might say "But wait! Foo(double i) doesn't have the same signature as Foo(int i), so it shouldn't be removed from the set!".

However, because there is an implicit conversion from int to double, it is considered to have the same signature, so Foo(int i) is removed from the set.

Is it correct to call a base class method on derived class objects when overloading output / input operator for derived class?

Well that's the basic idea of inheritance. child is a parent and therefore has a method float get_pr() const. So it's totally fine to call a base class method from a derived class.

A derived class (child in your case) inherits everything from it's parent class and can access every public and protected member of it.

Overloading virtual method in derived class with different signature

print in the DERIVED shadows print in the BASE, even though the signatures are different.

To fix, add using BASE::print; to the DERIVED. Note that this line can change the access modifier of the inherited function; if you want the function to be public, the using ... must also be public.

Be aware that you don't override any functions here (usually that's only possible if signatures are same). You create two unrelated functions with the same name. This means virtual can be removed, unless you plan to add more derived classes and actually override functions in them.

Can't call base class's method when another overload present in derived class

Add this line:

using A::Foo;

in class B (public part) and it will work.

In this case Foo from B hides Foo from A. One of the C++ strange behaviors.



Related Topics



Leave a reply



Submit