Classes part 16 - Virtual Functions (Dynamic dispatch) | Modern Cpp Series Ep. 52

preview_player
Показать описание

►Lesson Description: In this lesson I show you how to ensure that the correct member functions are called when instantiating objects at run-time. Inheritance-based polymorphism (i.e. the ability to decide what the object type is at run-time, from any type in your inheritance hierarchy) is a powerful tool. That said, with great power comes great responsibility to ensure the right member functions are then called from the instantiated type. The keywords 'virtual' and 'override' in C++ (override being available in modern C++) enable 'dynamic dispatch' (the ability to choose the correct member function at runtime) to work.

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

Best explanation on virtual / override class methods. 😀👍😁🤗

tomastank
Автор

Ah, Thank You So Much. This playlist is gonna be big in the near future ikt !

tanishq
Автор

Very neatly explained as always. Thank You.

dhanushs
Автор

Hallo Mike, as always i have liked the video. I cant wait for the virtual table video, it would me to see behind the scene and wrap my head around polymorphism at run time.

blaisofotso
Автор

7:40 why only Base destructor was called?

nigelwan
Автор

Hi Mike, I am following the full series currently. Thank you so much for nice explained videos. at 12:02, what happened if both memberFunc() has different parameters then what is the role of override in derived class. like func(int x) in base, func(long x) in derived class... Thank you so much for your time.

bullikoti
Автор

Hello Mike, Thanks for the video.
Snippet-1 works, but snippet-2 throws this error: 'virtual' can only be specified inside the class definition

// SNIPPET-1
class BaseClass
{
public:
BaseClass();
~BaseClass();
virtual void memberFunction()
{
cout << "BaseClass memberFunction Constructor." << endl;
}
};

// SNIPPET - 2
class BaseClass
{
public:
BaseClass();
~BaseClass();
void memberFunction();
};
virtual void BaseClass::memberFunction()
{
cout << "BaseClass memberFunction Constructor." << endl;
}

BalaganeshS-zk
Автор

Hey, is there any reason that the destructor for erived is never called? I assume it revolves around the dynamic memory allocation and using delete, but wouldn't the delete on the memory allocation cause the destructor to be called? Thanks!

thicc_tiffanyplayz
Автор

Sir can you please make a video on upCasting and downCasting because i was not able to understand that that : Base* instance = new Derived;

crazymemes
Автор

a complete example here:
#include <iostream>

#include <string>
using namespace std;

class EntityBase
{
public:
EntityBase(std::string name) : m_name{name}
{
std::cout << "***EntityBase *** constructed with name \t" << m_name << endl;
}
~EntityBase()
{
std::cout << "***EntityBase*** destroyed\t" << m_name << endl;
}
virtual void f()
{
std::cout << "f() in ***EntityBase*** class\n";
}
virtual void print()
{
std::cout << "Inside ***EntityBase*** print(): name:\t" << m_name << endl;
}

protected:
std::string m_name;
};

class Monster : public EntityBase
{
public:
Monster(int a) : EntityBase("default")
{
this->age = a;
std::cout << "Monster constructed with 1 arg\n";
}
Monster(int a, std::string name) : EntityBase(name)
{
this->age = a;
std::cout << "Monster constructed with 2 args\n";
}
~Monster()
{
std::cout << "Monster destroyed\n";
}
void printAge()
{
std::cout << "Monster age:\t" << this->age << endl;
}
void f() override
{
// EntityBase::f();
std::cout << "f() in Monster class\n";
}
void print() override
{
// EntityBase::print();
std::cout << "Monster name:\t" << m_name << '\t';
this->printAge();
}

private:
int age;
};

int main()
{
// Monster badMonestrer(424);
// badMonestrer.print();
// badMonestrer.f();
EntityBase * ptr; //: EntityBase or Monster
ptr = new Monster(333, "Monster name1");
ptr->print();
ptr->f();
delete ptr;
// Monster badMonestrer2(333, "Monster name 2");
// badMonestrer2.print();
return 0;
}

mrezahoseiny
Автор

A small request can u please help me on stack overflow i read that we use entity component system to overcome the impact of virtual function but what does that exactly mean?

jugalshah
visit shbcf.ru