Call derived method from base pointer [duplicate] - c++

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

Related

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

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

Can we have a static virtual functions? If not, then WHY? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ static virtual members?
Can we have a static virtual functions? If not, then WHY?
class X
{
public:
virtual static void fun(){} // Why we cant have static virtual function in C++?
};
No, because it doesn't make any sense in C++.
Virtual functions are invoked when you have a pointer/reference to an instance of a class. Static functions aren't tied to a particular instance, they're tied to a class. C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.
That would make no sense. The point of virtual member functions is that they are dispatched based on the dynamic type of the object instance on which they are called. On the other hand, static functions are not related to any instances and are rather a property of the class. Thus it makes no sense for them to be virtual. If you must, you can use a non-static dispatcher:
struct Base
{
static void foo(Base & b) { /*...*/ }
virtual ~Base() { }
virtual void call_static() { foo(*this); /* or whatever */ }
};
struct Derived : Base
{
static void bar(int a, bool b) { /* ... */ }
virtual void call_static() { bar(12, false); }
};
Usage:
Base & b = get_instance();
b.call_static(); // dispatched dynamically
// Normal use of statics:
Base::foo(b);
Derived::bar(-8, true);

c++: Call derrived function from base constructor? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Calling virtual functions inside constructors
class Base
{
virtual void method()
{ cout << "Run by the base."; };
public:
Base() { method(); };
};
class Derived: public Base
{
void method()
{ cout << "Run by the derived."; };
};
void main()
{
Derived();
}
Output:
Run by the base.
How can one have the derived method run instead, without making a derived constructor?
Calling virtual functions inside constructors
http://www.artima.com/cppsource/nevercall.html
You can't since the "derived" part of the object has not been constructed yet so calling a member function from it would be undefined behavior.
You can't do this without adding extra code.
A common way to achieve this is to use a private constructor and a create function that first calls the constructor (via new) and then a second finish_init method on the newly created object. This does prevent you from creating instances of the object on the stack though.