C++ Issue with Function Overloading in an Inherited Class

c++ issue with function overloading in an inherited class

All you need is a using:

class classB:public classA{ 
public:
using classA::func;
void func(int){};
};

It doesn't search the base class for func because it already found one in the derived class. The using statement brings the other overload into the same scope so that it can participate in overload resolution.

Inheritance and function overloading

Why is it possible to call b.hello() but not b.test()?

There are member functions with the name test in both classes A and B. However, classes are scopes, and functions do not overload across scopes. Therefore, the overloaded set for the function test in B consists only of test(int).

A member function with the name hello is, on the other hand, only present in class A, and B inherits this member function.


Note however that it is still possible to call A::test() on b:

B b;
b.A::test();

You can also bring A::test to the scope introduced by B with the using declaration:

class B: public A {
public:
using A::test; // brings A::test() to this scope
void test(int a) override {}
};

Now, A::test() can be called directly on b since the overload set for the function test in B consist of both test() and test(int):

B b;
b.test(); // calls A::test()
b.test(1); // calls B::test(int)

can't find overloaded method from inherited class template

class A_der : public A<B_der>
{
void init() override;
};

When you declare a function init in the derived class, it hides all things named init from the base class. This is just like when declaring something in an inner scope - it hides things with the same name from outer scopes.

There are ways to import the hidden names, but an easy solution would be to just chose a different name, like init_base. Or, probably better, pass a parameter to the class constructor.

C++ inheritance overloads functions with different parameters

You can use the using declaration in the derived class like

using Base::foo;

to make visible in the derived class the overloaded function(s) foo declared in the base class.

Here is your program within which the using declaration is inserted.

#include <iostream>

class Base
{
public:
Base() = default;

//base const char* overload
void foo(const char* message)
{
std::cout << message << std::endl;
}
//other overloads ...
};
class Derived : public Base
{
public:
Derived() = default;

using Base::foo;
//derived int overload
void foo(int number)
{
std::cout << number << std::endl;
}
};

int main()
{
Derived b;
b.foo(10); //derived overload works
b.foo("hi"); //causes error, acts as if not being inherited from Base class
return 0;
}

The program output is

10
hi

C++ Method overloading by Inherited Parameter Type

The derived function will be called and used because it matches this "DoSomething(Derived d)"
signature.

Have you consider using the code like this instead:

#include<iostream>
using namespace std;
class Base {
public:
virtual void DoSomething();
};

class Derived : public Base {
public:
void DoSomething() override;
};
void Base:: DoSomething() {
cout << "Do Something to Base" << endl;
}

void Derived :: DoSomething() {
cout << "Do Something to Derived" << endl;
}
int main() {
Base *d = new Derived();
d->DoSomething();
delete d;
return 0;
}

It accomplished the same task, and allows you to take advantage of polymorphisms strength.

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);}
// ...
};

Overload a virtual method in a derived class so it takes more parameters in C++

You can use the overloaded function of B only if the pointer to use is of type B.

See:

#include <iostream>
#include <memory>

class A{
public:
virtual void foo(int a) = 0;
};

class B : public A
{
public:
virtual void foo(int a) override
{

}
void foo(int a, int b)
{
std::cout << a << "," << b;
}
};

int main(){
auto b = std::make_shared<B>();
b->foo(1, 2);

//to use a:
A* aPtr = b.get();
aPtr->foo(1);

return 0;
}


Related Topics



Leave a reply



Submit