Files
Polymorphism/main.cpp
2025-02-08 20:21:42 -05:00

29 lines
781 B
C++

#include <someFunc.h>
auto* b = new BaseClass; //Allocate an instance of each class from the heap,
auto* dc1 = new DerivedClass1; //Doing this is *really* slow compared to the stack.
auto* dc2 = new DerivedClass2; //But it's fantastic for things you'll allocate once and use a LOT.
int main() { //Look at polymorphicClass.h.
someFunc(b);
someFunc(dc1);
someFunc(dc2);
delete b; //Delete them when you're done so that you don't mem-leak.
delete dc1;
delete dc2;
//b->printClassName(); //Don't access them after you delete them.
//This is called a "use after free" and is very bad.
return 0;
}
#pragma region windows
#ifdef _WIN32
extern "C" {
int wmain(int argc, wchar_t* argv[]) {
return main();
}
}
#endif
#pragma endregion