Anonymous
But it can be not one
Anonymous
Anonymous
Anonymous
TO create object we need complete information.But, while we are using virtual, we do not have vptr and vtable at the point of object creation
Anonymous
Anonymous
Anonymous
We need to know the exact type of the object when we are creating object.
Anonymous
Anonymous
You said "Because destructor is not in virtual table of a class". Yes, it is not then what?
Anonymous
Anonymous
destructor
Anonymous
Anonymous
If we don't know the exact type then we can call only methods in which we 100% sure, right?
Base* can refer as to a Base object as to a Derived one
Anonymous
I will explain it here.
Anonymous
wait
Anonymous
constructor deals with object creation thus it can't be virtual
Anonymous
cause you should know the exact type of object while creating
Anonymous
Whenever we have
base *b = new Derived();
then if we call any non-virtual function as b->fun(), then at compile time it will run the fun() of base class due to compile time poly.
Same is the case with destructor as well
If we make fun() as virtual then it will run the child class fun() due to RUN-time poly.
So, we need to make destructor is virtual so that first the child class destructor is executed first.
Is it completely correct Or do I need to add more.
KINDLY REPLY!!
Anonymous
olli
Anonymous
I GUESS COMPILE TIME POLYMORPHISM = EARLY BINDING.
Isn't it?
olli
Anonymous
Anonymous
I mean early binding happens in compile time poly.
olli
https://en.cppreference.com/w/cpp/language/virtual
Anonymous
Nowhere did they specify early binding in the article.
olli
early binding = direct function call = not polymorphic
late binding = (runtime) polymorphic
How much do you know about CS?
Anonymous
I meant early binding happens in compile time poly
Anonymous
so what is an example of compile time poly without direct function call?
olli
olli
Neither any academia I trust nor the standard define this term
Thor
Guys,,
Is there any online website to check our knowledge about c/c++?
olli
Thor
Thanks❤️
Anonymous
doesn't a simple call to non-virtual function fall under compile-time polymorphism
Anonymous
?
Anonymous
yeah it shouldn't be polymorphic.
I accept my Bad.
overloading should be compile time poly
olli
Anonymous
olli
define "polymorphism"
Anonymous
having different forms of a thing.
Pietrø
Can anyone suggest me a good IDE for Mac?
Pietrø
When I Had windows I used dev c++
Pietrø
Basically I have to go to the directory of the file, and then?
Anonymous
use : g++ filename.cpp
Anonymous
to compile
Anonymous
and to run use ./a.out
Pietrø
g++ is used for compiling right?
Pietrø
I mean, it is the command
Anonymous
yep
Anonymous
yeah
olli
In a nutshell Polymorphism is the Ability of type A to appear as and be used like another type B
Anonymous
Anonymous
Pietrø
What if I have many files?
olli
yes
so how does it apply to overloading?
Anonymous
so how does it apply to overloading?
I meant that overloading is compile time polymorphism and I accept that it was my mistake to say that calling a non-virtual function is compile time polymorphism.
My bad.
olli
Anonymous
In overloading you use different functions with same name and different parameter types to do some specific job.
olli
struct X {
void foo(int) {}
void foo() {}
};
int main() {
X x{};
x.foo(3);
}
Do I use x different when I call foo or foo(3) ?
Does any of these call make it pretend to be something else?
Anonymous
So same function name but different param type to do different jobs
olli
Anonymous
No you don't use x differently.
olli
so it's not poylmorphic
Anonymous
Anonymous
I thought if we have different functions with same name but different param types or different no. of param types then
Anonymous
these we would be following function overloading.
olli
yes, function overloading, but not polymorphism
olli
these are two different things
olli
in C++ you can actually achieve "Static" or Compile time polymorphism by using CRTP. e.g.
template <class D> struct Base {
void foo() {
static_cast<D*>(this)->foo();
}
};
struct X : Base<X> {
void foo() {}
};
Anonymous
I'm yet to learn about static casting and templates. I won't understand it now.