Reputation: 147
I want to understand the purpose of virtual functions.
Lets analyse this code where the member function is non-virtual:
EXAMPLE 1:
struct A
{
void foo1() { cout << "foo 1 from A \n"; }
};
struct B : A
{
void foo1() { cout << "foo 1 from B \n"; }
};
int main()
{
A *a = new B;
a->foo1(); // will print "foo 1 from A \n" because A::foo1 isn't virtual
B *b = new B;
b->foo1(); // will print "foo 1 from B \n"
}
As we see the output of a->foo1()
; will be "foo 1 from A" but I want to function B executes its own foo1
function. So I must overridde it and make A::foo1
virtual function.
EXAMPLE 2:
struct A
{
virtual void foo1() { cout << "foo 1 from A \n"; }
};
struct B : A
{
void foo1() { cout << "foo 1 from B \n"; }
};
int main()
{
A *a = new B;
a->foo1(); // will print "foo 1 from B \n"
B *b = new B;
b->foo1(); // will print "foo 1 from B \n"
}
Now A::foo1
was overridden and a->foo1()
prints "foo 1 from B" as we want it to. However, let's consider the case where class B has some functions not present in A:
EXAMPLE 3:
struct A
{
int a;
void foo1() { cout << "foo 1 from A \n"; }
};
struct B : A
{
int b;
void foo1() { cout << "foo 1 from B \n"; }
void foo2() { cout << "foo 2 from B \n"; }
};
int main()
{
A *a = new B;
// a->foo2(); // compiler error, a doesn't see foo2 function
a->foo1();
// a->b = 1; // compiler error, a doesn't see member variable b
a->a = 1;
// We aren't going to do B *b = new A; here because that's nonsense
B *b = new B;
b->foo2(); // ok
b->foo1(); // ok
b->b = 1; // ok
b->a = 1; // ok
}
As we see now B isn't an exact copy of A; it inherits A and extends it with some new functions and variables. We can't do a->foo2()
or a->b
.
I think statements like A *a = new B;
are much more confusing when reading or analysing the code than B *b = new B;
.
I know that b
is pointer to an instance of B. Is it not confusing to wonder about what the type of an object pointed to by an A*
is?
So my question is: what could we use virtual functions for?
I won't use it with derived class if they have variables or functions not present in the base class. It's much clearer to create a new object using B *b = new B;
, not A *a = new B;
.
When I create an instance of B using B *b = new B;
, the member functions of A do not need to be virtual, because B will automatically override A::foo1
with its own foo1
.
Therefore, I think the only use for virtual functions is when we want to change the functionality at runtime.
Perhaps it could be useful in a case like this, when we want to change the executed class at runtime:
A *a;
B b;
C c;
a = &B;
a->foo1();
a = &C;
a->foo1();
Alternatively, this may be useful when passing the argument to a function:
void execute(A *a)
{
a->foo1();
}
Am I missing something about this?
Upvotes: 0
Views: 312
Reputation: 2523
Virtual function is the way in which C++ implements the polymorphish concept.
In C++, simple method overloading is used for compile time polymorphism, in which compiler binds the methods during the compilation.
And in case of virtual function, the compiler will use the methods during the runtime as and when required.
Upvotes: 0
Reputation: 18449
Virtual functions are the main way C++ implements run time polymorphism. A Google search for polymorphism may show you what is useful about the technique.
Upvotes: 3