Mixing overriding and overloading in C++ [duplicate] - c++

This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 3 years ago.
I don't understand why I have this error error: no matching function for call to ‘Child::m(Mother&) when I try to compile my code.
From what I understand:
since the type of c is Child and Child::m have a parameter of type Child while m is of type Mother in c.m(m) then it's the function m() from the Mother class who need to be called.
class Mother{
public:
void m(const Mother&) {
}
};
class Child: public Mother{
public:
void m(const Child&) {
}
};
int main() {
Mother m;
Child c;
c.m(m);
}

Your Child class has no member function m that takes a Mother. It is hidden by the m declared in Child. You can unhide it by adding using Mother::m; in Child.

Related

Calling a function of the derived class from a variable type of parent class [duplicate]

This question already has answers here:
Unable to call derived class function using base class pointers
(3 answers)
Base-class pointer pointing to a derived class cannot access derived-class method
(3 answers)
Closed 4 months ago.
I am learning Object Oriented Programming. I wrote the following simple program, but I don't follow why the call b->derived_function(); doesn't work. I understand the base class Base does not have a member function with that name, but since I do: b = new Derived();, shouldn't b be an object of the Derived class (making the function call valid)?
#include <iostream>
using namespace std;
class Base {
public:
virtual void base_function() = 0;
};
class Derived: public Base {
public:
void base_function() {
cout<<"base_function"<<"\n";
}
void derived_function() {
cout<<"derived_function"<<"\n";
}
};
int main() {
Base* b;
b = new Derived();
// b->base_function(); //works
b->derived_function(); //doesn't work; why?
return 0;
}
Here's a repro on ideone.com. Thanks!

overriding function not worked [duplicate]

This question already has answers here:
What is object slicing?
(18 answers)
Why doesn't polymorphism work without pointers/references?
(6 answers)
Closed 2 years ago.
I have a base and derived class which derived class is overriding a function in base class
struct Base{
virtual void action(){}
}
struct Derived:public Base{
virtual void action() override{}
}
I have another class that uses Base interface like following
struct Observable{
deque<Base> myObjects;
void addObject(Base &base){
myObjects.push_back(base);
}
void notify(){
for (auto it = myObjects.begin(); it != myObjects.end(); it++) {
it.action();
}
}
}
In main I initialized observer class like this
Derived myDeived;
Observable observable;
observable.addObject(myDerived);
observable.notify();
the problem when I call "notify" and as a consequence it call all "action" function in array It does not call "action" function overrided in Derived but in Base class? please explain me why this happened and how to correct it?
Thank you

C++ Overload & Override - cannot initialize a parameter of type '' with an rvalue of type '' [duplicate]

This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 5 years ago.
I'm normally a C# guy, and it handles this fine, largely under its handling of "best match" resolving the method to call. I'm trying to do something similar in C++ now, but am getting a compile error. Long story short, it's a combination of method overloading and overriding.
class Bar : public Foo { } //Contents don't really matter here
class Base
{
public:
virtual void Do(Foo* foo) { }
virtual void Do(Bar* bar) { }
};
class Derived : public Base
{
public:
virtual void Do(Bar* bar) { }
}
Foo* foo = new Foo();
Derived* d = new Derived();
d->Do(foo); //cannot initialize a parameter of type 'Bar*' with an rvalue of type 'Foo*'
So, it tries to resolve the method against only the methods in Derived rather than recognizing the base class implements a valid match. Again, C# finds the base method. Can C++ not do this, or am I missing something?
A function in a derived class hides any functions of the same name in a base class (even if you're overriding a virtual function). To unhide the other overloads, use a "using" statement:
class Derived : public Base
{
public:
virtual void Do(Bar* bar) { }
using Base::Do;
};

Best way to call hidden base class method [duplicate]

This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 9 years ago.
In the following code, "d.Foo()" throws a compiler error claiming that function Foo() does not take 0 arguments. Yet a 0-argument function with that name exists in the base class. The line "d.Base::Foo()" is acceptable.
I have a vague memory of learning that the use of a function name in a derived class hides all functions of that name in a base class, even though arguments may be different. I don't remember why, nor do I remember the best way to avoid that problem. Is my solution best, or is there another way to get at Base::Foo()?
Thanks very much!
RobR
// Override.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
class Base
{
public :
void Foo()
{
}
};
class Derived : public Base
{
public:
void Foo(int x)
{
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived d;
d.Foo();
d.Base::Foo();
return 0;
}
you can use(!) base class member functions via using
class Derived : public Base {
public:
using Base::Foo;
};
You could define Derived::Foo() as:
class Derived : public Base {
public:
void Foo() { Base::Foo(); }
};

Grandparent overloaded function in child [duplicate]

This question already has answers here:
Function with same name but different signature in derived class not found
(2 answers)
Closed 5 years ago.
I need to understand why C++ don't allow to access Grandparent overloaded functions in Child if any of the overloaded function is declared in Parent. Consider the following example:
class grandparent{
public:
void foo();
void foo(int);
void test();
};
class parent : public grandparent{
public:
void foo();
};
class child : public parent{
public:
child(){
//foo(1); //not accessible
test(); //accessible
}
};
Here, two functions foo() and foo(int) are overloaded functions in Grandparent. But foo(int) is not accessible since foo() is declared in Parent (doesn't matter if it declared is public or private or protected). However, test() is accessible, which is right as per OOP.
I need to know the reason of this behavior.
The reason is method hiding.
When you declare a method with the same name in a derived class, base class methods with that name are hidden. The full signature doesn't matter (i.e. cv-qualifiers or argument list).
If you explicitly want to allow the call, you can use
using grandparent::foo;
inside parent.
Just imagine that a library has this class:
struct Base {
};
In your code you use that class as a base class:
struct Derived : Base {
void f(int);
};
and now you write:
Derived d;
d.f('a');
And now you get the shiny new version 2.0 of that library, and the base class has changed a bit:
struct Base {
void f(char);
}
If overloading applied here, your code would break.