overriding function not worked [duplicate] - c++

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

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!

Mixing overriding and overloading in C++ [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 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.

Call derived method from base pointer [duplicate]

This question already has answers here:
Calling virtual functions inside constructors
(15 answers)
Closed 7 years ago.
I'd like to do the following:
void do_stuff(Base* base_ptr) {
// here I need the overridden methods
base_ptr->init();
}
class Base {
Base() {
do_stuff(this);
}
virtual void init() {}
};
class Derived : public Base {
virtual void init() override {
// Derived specific init
}
}
But all I get are calls to Base::init(), is it even possible to do what I intend?
You are calling a virtual function from within the constructor!
Duplicated of -> Calling virtual functions inside constructors

Calling an overloaded function from a derived class [duplicate]

This question already has answers here:
How to call a parent class function from derived class function?
(7 answers)
Closed 7 years ago.
C++ newbie here. Is there a way to call an overloaded function in a base class from the same function name with the same signature in a derived class? In Smalltalk, I can do it the "super" keyword. Is there any C++ equivalent?
class MyBaseClass {
void initialize() { doSomething(); }
};
class MyDerivedClass : public MyBaseClass {
void initialize() {
super initialize(); // first, call MyBaseClass::initialize()
doLocalInitialize(); // now initialize non-inherited members
}
Thanks,
Norm
You literally said the correct syntax in your comment :)
class MyBaseClass {
void initialize() { doSomething(); }
};
class MyDerivedClass : public MyBaseClass {
void initialize() {
MyBaseClass::initialize(); // first, call MyBaseClass::initialize()
doLocalInitialize(); // now initialize non-inherited members
}

Virtual function call in constructor [duplicate]

This question already has answers here:
C++ virtual function from constructor [duplicate]
(7 answers)
Closed 9 years ago.
I have this layout
class Base {
public:
virtual void Initialize() { // base Implementation }
Base() { Initialize(); }
};
class der_1 : public Base
{
public:
der_1() : Base() {}
virtual void Initialize() { // der_1 Implementation }
};
class der_2 : public Base
{
public:
der_2() : Base() {}
virtual void Initialize() { // der_2 Implementation }
};
Now, whenever I create a new object of class der_1 or der_2, I will end up calling the base implementation of Initialize(). Apparently, I can't call a virtual function while the object is being created.
As of now, I am calling the Initialize function after I create the object of type der_1 or der_2, which doesn't seem a correct practice to me as that will couple the Initialize function call to each time an object is created.
Can someone suggest me better alternatives?
During the constructor call, the object "is" still only an instance of the base class, so it does know about your overloaded Initialize() function.
There are some suggestions for dealing with the situation here:
C++ virtual function from constructor