CodingNovice
Programmer
On the whole I understand virtual functions but there is one thing I cant fathom....
virtuality.h
#include <iostream>
using std::cout;
using std::endl;
class A
{
public:
virtual ~A() {}
virtual void f() { cout << "A::f()" << endl; }
};
class B : public A
{
public:
virtual ~B() {}
virtual void f() { cout << "B::f()" << endl; }
};
class C : public B
{
public:
virtual ~C() {}
virtual void f() { cout << "C::f()" << endl; }
};
class D : public C
{
public:
virtual ~D() {}
virtual void f() { cout << "D::f()" << endl; }
};
virtuality.cpp
#include "virtuality.h"
int main()
{
A* array[4] = { new C, new B, new A, new D };
B* array2[3] = { new B, new D, new C };
for( int i=0;i<4;++i)
{
array->f();
delete array;
}
for ( int i=0;i<3;++i)
{
(*array2).f();
delete array2;
}
std::cin.get();
return 0;
}
Now here in the first loop I call a virtual function through a pointer so dispatch is virtual. In the second loop I call a virtual function through an object. So shouldn't the second loop call B::f() 3 times? However it doesnt. It seems that that also is virtually dispatched. Thinking in c++ (my text book) says that virtual functions called through objects are early bound. So why does my second loop late bind?
I would like to be 100% sure in my head that when I use virtual functions I know exactly which function will be called. My compiler is telling me that im wrong here but i dont understand why.
I have an array with 3 B* in it that point to various derivatives of B. Surely when I dereference a B*, to get an object, I should get a B no matter what the dynamic type of the object really is. So calling f() through an object of type B should call B::f(). So why is this not happening? Where am I going wrong in my understanding?
virtuality.h
#include <iostream>
using std::cout;
using std::endl;
class A
{
public:
virtual ~A() {}
virtual void f() { cout << "A::f()" << endl; }
};
class B : public A
{
public:
virtual ~B() {}
virtual void f() { cout << "B::f()" << endl; }
};
class C : public B
{
public:
virtual ~C() {}
virtual void f() { cout << "C::f()" << endl; }
};
class D : public C
{
public:
virtual ~D() {}
virtual void f() { cout << "D::f()" << endl; }
};
virtuality.cpp
#include "virtuality.h"
int main()
{
A* array[4] = { new C, new B, new A, new D };
B* array2[3] = { new B, new D, new C };
for( int i=0;i<4;++i)
{
array->f();
delete array;
}
for ( int i=0;i<3;++i)
{
(*array2).f();
delete array2;
}
std::cin.get();
return 0;
}
Now here in the first loop I call a virtual function through a pointer so dispatch is virtual. In the second loop I call a virtual function through an object. So shouldn't the second loop call B::f() 3 times? However it doesnt. It seems that that also is virtually dispatched. Thinking in c++ (my text book) says that virtual functions called through objects are early bound. So why does my second loop late bind?
I would like to be 100% sure in my head that when I use virtual functions I know exactly which function will be called. My compiler is telling me that im wrong here but i dont understand why.
I have an array with 3 B* in it that point to various derivatives of B. Surely when I dereference a B*, to get an object, I should get a B no matter what the dynamic type of the object really is. So calling f() through an object of type B should call B::f(). So why is this not happening? Where am I going wrong in my understanding?