Consider the below code.
#include<iostream>
using namespace std;
class Base
{
public:
virtual void function1() {cout<<"Base:function1()\n";};
virtual void function2() {cout<<"Base:function1()\n";};
};
class D1: public Base
{
public:
virtual void function1() {cout<<"D1:function1()\n";};
virtual void function2() {cout<<"D1:function2()\n";};
};
int main()
{
Base *ptr= new D1;
ptr->function1();
ptr->function2();
return 0;
}
The ptr will be pointing to D1 obj.
so whenever i call ptr->function1(), the function address is fetched from virtual table of class D1. It works same way for ptr->function2() as well.
In this case the vtable[0] will have function pointer to function1(), vtable[1] will have function pointer to function2().
My question is how function call to vtable index mapping happens?
How ptr->function1() & ptr->function2() indexes to vtable[0] & vtable[1] respectively?
The first element of the class is usuaylly the (hidden) vtable pointer - vptr. For a polymorphic class, the vtable is first initialized to the vtable of base class in the base class constructor. Then, when the derived class constructor executes the same vtable pointer is initialized to point to the derived class vtable. Note that the base class vtable points to base version of function1 and function2 whereas derived class vtable points to derived version of function1 and function2.
Now, when a pointer to base class points to an instance of derived class, this is what 'typically may' happen:
class base
{
//int* vptr; //hidden vtable pointer, created by compiler for polymorphic class. vptr points to base class vtable for base clas objects
public:
virtual void function1(){std::cout <<"base::function1()"<<std::endl;}
virtual void function2(){std::cout <<"base::function2()"<<std::endl;}
};
class derived: public base
{
//int* vptr; //hidden vtable pointer, inherited from the base class. vptr points to derived class vtable for derived class objects
public:
virtual void function1(){std::cout <<"derived::function1()"<<std::endl;}
virtual void function2(){std::cout <<"derived::function2()"<<std::endl;}
};
int main()
{
typedef void (*vtableFnPtr)();
base* pBase;
base base_obj;
derived derived_obj;
pBase = &derived_obj; //base pointer pointing to derived object
//one of the several possible implementations by compiler
int* vtableCallBack = *(int**)&derived_obj; //read the address of vtable pointed by the hidden vptr in the derived_obj
//pBase->function1();
((vtableFnPtr)vtableCallBack[0])(); //calls derived::function1(), when application calls pBase->function1();
//pBase->function2();
((vtableFnPtr)vtableCallBack[1])(); //calls derived::function2(), when application calls pBase->function2();
pBase = &base_obj;
//one of the several possible implementations by compiler
vtableCallBack = *(int**)&base_obj; //base pointer pointing to base object
//pBase->function1();
((vtableFnPtr)vtableCallBack[0])(); //calls base::function1(), when application calls pBase->function1();
//pBase->function2();
((vtableFnPtr)vtableCallBack[1])(); //calls base::function2(), when application calls pBase->function2();
}
Note that the C++ compiler does not say anything about the implementation methodology to be used for achieving polymorphic behavior and hence it is completly upto the compiler to use vtable or any other implementation as long as the behavior is polymorphic. However, vtable remains one of the most widely used methods to achieve polymorphic behavior.
"All problems in computer science can be solved by another level of indirection"
I referred to excellent and detail blog post and have tried below to explain use of vtable for your simple example. See if you are read thorough it.
struct Base;
// enumerates all virtual functions of A
struct table_Base {
void (*function1)(struct Base *this);
void (*function2)(struct Base *this);
};
struct Base {
const struct table_Base *pvtable; // table maintains pointers to virtual functions. Eventually to implementations
int data;
};
void Base_function1(struct Base *this) {
std::cout << "Base:function1()" << std::endl;
}
void Base_function2(struct Base *this) {
std::cout << "Base:function2()" << std::endl;
}
// table data for Base
static const struct table_Base table_Base_for_Base = { Base_function1, Base_function2};
void Base_Ctor(struct Base *this) {
this->pvtable = &table_Base_for_Base;
this->data = 1;
}
// Now for class D1
struct D1;
struct table_D1 {
void (*function1)(struct D1 *this);
void (*function2)(struct D1 *this);
};
struct D1 {
struct Base base;
const struct table_D1 *pvtable;
int more_data;
};
void D1_function1(struct D1 *this) {
std::cout << "D1:function1()" << std::endl;
}
void D1_function2(struct D1 *this) {
std::cout << "D1:function2()" << std::endl;
}
// Important functions that do re-direction
void D1Base_function1(struct Base *this) {
D1_function1((struct D1*) this);
}
void D1Base_function2(struct Base *this) {
D1_function2((struct D1*) this);
}
// table data for D1
static const struct table_D1 table_D1_for_D1 = {D1_function1, D1_function2};
// IMPORTANT table
static const struct table_Base table_Base_for_D1 = {D1Base_function1, D1Base_function2};
// Constructor for derived class D1
void D1_Ctor(struct D1 *this)
{
Base_Ctor(&this->base); // Base class vtable is initialized.
// Now, Override virtual function pointers
this->base.vtbl = &table_Base_for_D1; // Replace the vtable
this->mode_data = 100;
}
Internally this is the logic followed by the compiler to achieve correct behavior for virtual functions.
ptr->function1();
ptr->function2();
You can trace the method calls above using vtable logic explained and see if it works.
Related
I have a base class which serves as an interface (if I use that word correctly). The idea is that the base class has some derived classes that implement one virtual function of the base class. Then I also need another class that extends the base class (lets call it extended base). What I would like is that I can store a class derived from base into an extended base pointer.
MWE:
class Base {
public:
virtual ~Base();
virtual double value();
}
class Derived : public Base{
public:
double value() override {return 5;}
}
class ExtendedBase : public Base {
public:
virtual ~ExtendedBase ();
virtual double value2(){return 10;}
}
int main() {
ExtendedBase * object;
object = new Derived();
std::cout << object->value(); //should give implementation in Derived, i.e. 5
std::cout << object->value2(); //should give implementation in ExtendedBase, i.e. 10
delete object;
return 0;
}
With this MWE I get a compile error at the second line in the main. error: cannot convert 'Derived*' to 'ExtendedBase*' in assignment object = new Derived();. Part of me understands why it doesn't work (although I can't explain), but I would like to know if I can get the desired behaviour in some other way.
P.S. Sorry about the bad question name, I couldn't think of another way to keep it short
P.S.2 I know raw pointers like this are not advised. In the future I will change to smart pointers but I don't think they are needed for this simple example
ExtendedBase and Derived are each derived from Base. If you want to use an ExtendedBase* pointer to point to a Derived object, you will need to derive Derived from ExtendedBase.
To use a different example,
class Feline{
virtual void run();
}
class Housecat : Feline{
void run() {}
}
class BigCat : Feline{
virtual void run();
virtual void roar();
}
Here Feline, Housecat, and BigCat are analogous to Base, Derived, and ExtendedBase. BigCat and Housecat are each Feline, but since Housecat is not a BigCat, you can't use a BigCat* pointer to point to a Housecat.
This is the desired behavior from a language architect perspective.
For instance, if you have
class Ship
{
public:
virtual void move() = 0;
}
class Steamboat : public Ship
{
public:
virtual void move() override { ... }
}
class Sailboat : public Ship
{
public:
virtual void move() override { ... }
virtual void setSails() { ... }
}
Now, you don't want a Steamboat to become a Sailboat all of a sudden, hence:
Steamboat* tootoo = new Sailboat;
cannot be valid.
That's why your code cannot work. Conceptually.
So giving a quick fix is not possible, because your concept is not really clear.
When you are assigning an address to a pointer that means you should be able to access all the members of the type the pointer is pointing to through the pointer.
For ex,
class B {};
class D : B {};
B *p = new D();
now through p, at least you can access all the members of base portion of the derived class.
But in your code,
ExtendedBase * object;
object = new Derived();
object should be able to access all the members of ExtendedBase portion of the derived class. But how is it possible as derived class is not derived from ExtendeBase. So compiler is throwing error.
You need to do some changes in your code to work.
To make base as interface (abstract class), you need to define at
least one member function as pure virtual.
If you want to access the member function of ExtendedBase through
Base pointer, you should define same function 'val' in your
ExtendedBase.
Below are the changes.
#include <iostream>
using namespace std;
class Base {
public:
virtual ~Base() {};
virtual double value() = 0;
};
class Derived : public Base{
public:
~Derived() {};
double value() {
return 5;
}
};
class ExtendedBase : public Base {
public:
virtual ~ExtendedBase () {};
double value()
{
return 10;
}
};
int main() {
Base *p = new Derived();
std::cout << p->value() << std::endl;
delete p;
Base *p1 = new ExtendedBase();
std::cout << p1->value() << std::endl;
delete p1;
return 0;
}
#include <iostream>
class X{
public:
virtual void f() {std::cout << "1";}
};
class Y : public X{
public:
void f() {std::cout << "2";}
void g() {std::cout << "3";}
};
class Z : public Y{
public:
virtual void f() {std::cout << "4";}
void g() {std::cout << "5";}
virtual void k() {std::cout << "6";}
};
void main()
{
X *x = new Z;
Y *y = new Z;
Z *z = new Z;
x->f(); // 4
y->f(); // 4
y->g(); // 3
z->f(); // 4
z->g(); // 5
z->k(); // 6
system("PAUSE");
}
Output: 443456.
I got confused, why did it not print '2' when doing 'y->f()'? f() inside Y class isn't a virtual function.
I'd like to know more about it, thank you for the help.
why did it not print '2' when doing 'y->f(x)'? f() inside Y class isn't a virtual function.
Y::f is a virtual function. If a base class has a virtual function by the same name, then the derived class function is implicitly virtual as well.
First of all, overloading isn't relevant here--you don't have any overloaded functions (which would be functions with the same name, but different signatures, at the same scope).
Second, you're not really dealing much with virtual functions either. One one hand, it's true that you've (quite correctly) declared f() as a virtual function in your base class (X).
You've also overridden that virtual function in your derived class Y.
But, the primary time a virtual function means something is when you invoke the virtual function in an object of a derived class via a pointer (or reference) to the base class:
#include <iostream>
class base {
public:
virtual void f() { std::cout << "base::f()\n"; }
void g() { std::cout << "base::g()\n"; }
};
class derived : public base {
public:
virtual void f() { std::cout << "derived::f()\n"; }
virtual void g() { std::cout << "derived::g()\n"; }
};
int main() {
base *b = new base; // first case: pointer to base, base object
base *d = new derived; // second case: pointer to base, derived object
derived *d2 = new derived; // third case: pointer to derived, derived object
b->f(); // invokes base::f
b->g(); // invokes base::g
d->f(); // invokes derived::f
d->g(); // invokes base::g
d2->f(); // invokes derived::f
d2->g(); // invokes derived::g
}
So, in the first case, we have a base pointer, referring to a base object. With this, the derived class might as well not exist--we always get the base class member functions, regardless of whether they're virtual.
The case you asked about is pretty much the same as the third case in this code: we have a pointer to the derived object referring to an object of the derived class. Here again, it doesn't matter whether a function is virtual or not. Since we're using a pointer to a derived, we always get the derived version of each function, regardless of whether it's virtual or not.
The case where virtual functions get interesting is the second one, where we have a pointer to the base class, but it's referring to an object of the derived class. In this case, g() (which isn't virtual) is statically bound, so the function that gets called depends on the type of the pointer. Since we're using a pointer to base, we get base::g() even though the pointer is actually referring to a derived object.
But, with f() (which is virtual), we get "late binding"--even though we're using a pointer to base, when we invoke f(), we invoke derived::f(), because the object being pointed at is a derived object.
So yes, in your code, derived::f() is virtual because it's declared virtual in base--but even if it wasn't virtual, since you used a pointer to the derived class, you'd still have gotten derived::f() even if f() wasn't virtual at all.
Consider below line of code
#include<iostream>
using namespace std;
class base
{
int i;
public:
void printb()
{
cout << "This pointer of base "<<this<<endl;
}
};
class derived: public base
{
int i;
public:
void printd()
{
cout << "This pointer of derived "<<this<<endl;
}
};
main()
{
derived d1;
d1.printd();
d1.printb();
}
After compiling with g++(4.8.4) in 64bit ubuntu machine result is
This pointer of derived 0x7ffe74697ac0
This pointer of base 0x7ffe74697ac0
As per my understanding both base and derived this pointer will be same since we are calling with single object.
I added virtual keyword to printd() function of derived class as below
#include<iostream>
using namespace std;
class base
{
int i;
public:
void printb()
{
cout << "This pointer of base "<<this<<endl;
}
};
class derived: public base
{
int i;
public:
virtual void printd()
{
cout << "This pointer of derived "<<this<<endl;
}
};
main()
{
derived d1;
d1.printd();
d1.printb();
}
output of above code is as below
This pointer of derived 0x7ffee969b1d0
This pointer of base 0x7ffee969b1d8
Here this pointer value is different in derived and base even if calling with single object.Every time i run the program there is difference of 1byte between derived this pointer and base this pointer.
Can anybody tell why this difference in this pointer and how virtual keyword affect this pointer.
By adding the virtual keyword, you made derived polymorphic. A common implementation of runtime polymorphism is to add a pointer to the start of the object. This vptr points to a table of functions that are dynamically dispatched (commonly known as a vtable).
As such, the base sub-object, which is not polymorphic, is offset inside the derived super-object by the hidden pointer.
You see the pointer adjusted automatically, because the compiler injects code to perform this adjustment upon calling a member function. This ensures that printb will access all the (potential) members of base in the correct location.
If the virtual function table is the same for all objects of the class, then why can't the pointer to that table (vfptr) be static and be shared across all the objects?
The vtable is essentially static. But you need a vptr member actually inside the object to do virtual dispatch and other RTTI operations.
On a vptr implementation, this C++ code:
class Base {
public:
virtual void f();
};
class Derived : public Base {
public:
virtual void f();
};
may act similarly to something like this:
class Base {
public:
Base::Base() : m_vptr(&Base_vtab) {}
Base::Base(const Base& b) : m_vptr(&Base_vtab) {}
void f() { (m_vptr->f_impl)(this); }
protected:
struct VTable {
void (*f_impl)(Base* self);
};
const VTable* m_vptr;
static const VTable Base_vtab;
private:
static void Base_f(Base* self);
};
const Base::VTable Base::Base_vtab = { &Base::Base_f };
class Derived : public Base {
public:
Derived::Derived() : Base() { m_vtpr = &Derived_vtab; }
Derived::Derived(const Derived& d) : Base(d) { m_vptr = &Derived_vtab; }
Derived::~Derived() { m_vptr = &Base::Base_vtab; }
protected:
static const VTable Derived_vtab;
private:
static void Derived_f(Derived* self);
static void Derived_f(Base* self) { Derived_f(static_cast<Derived*>(self)); }
};
const Base::VTable Derived::Derived_vtab = { &Derived::Derived_f };
The virtual function table [assuming this is how the C++ compiler implements dynamic dispatch] is shared across all objects of a class. However, each object needs to know which virtual function table is relevant for this object. This is what the "virtual function table pointer" points to.
The basic idea is that the static type of a reference or a pointer to an object tells the compiler how a part of the virtual function table looks like. When it needs to do a virtual dispatch, it just follows this pointer and decides what function to call. Assume you have a base class B and derived classes D1 and D2 like this:
#include <iostream>
struct B {
virtual ~B() {}
virtual void f() = 0;
};
struct D1: public B {
void f() override { std::cout << "D1::f()\n"; }
};
struct D2: public B {
void f() override { std::cout << "D2::f()\n"; }
};
The virtual function table for D1 and D2 would contain a suitable pointer to D1::f() and D2::f() respectively. When the compiler sees a call through a pointer or reference to B to f() it needs to decide at run-time which function to call:
void g(B* base) {
base->f();
}
To resolve the call it looks at where there virtual function pointer points and calls the function n the appropriate slot (more or less; the contents of the virtual function table tend to be thunks which may do any necessary pointer adjustment, too).
"Virtual" means "determined at runtime". "Static" means "determined at translation time".
In order to make decisions at runtime, you have to have a parameter (such as the vptr) whose value can be set dynamically, at runtime. That is, for a given base object reference x, we need some value "x.vptr" that contains dynamic information (namely information about the most-derived class of which x is a base subobject).
class A
{
public:
virtual void Test();
...
};
class B: public A
{
public:
virtual void Test();
...
}
if vfptr is static for all objects, when compiling the below code:
void DoTest(A* pA)
{
...
}
A* pA = new B;
DoTest(pA);
A::vfptr will be recognized and used by compiler, but it is unexpected!
I have following
class base
{
};
class derived : public base
{
public:
derived() {}
void myFunc() { cout << "My derived function" << std::endl; }
};
now I have
base* pbase = new derived();
pbase->myFunc();
I am getting error myFunc is not a member function of base.
How to avoid this? and how to make myFunc get called?
Note I should have base class contain no function as it is part of design and above code is part of big function
If you are adamant that this function should NOT be a part of base, you have but 2 options to do it.
Either use a pointer to derived class
derived* pDerived = new derived();
pDerived->myFunc();
Or (uglier & vehemently discouraged) static_cast the pointer up to derived class type and then call the function
NOTE: To be used with caution. Only use when you are SURE of the type of the pointer you are casting, i.e. you are sure that pbase is a derived or a type derived from derived. In this particular case its ok, but im guessing this is only an example of the actual code.
base* pbase = new derived();
static_cast<derived*>(pbase)->myFunc();
myfunc needs to be accessible from the base class, so you would have to declare a public virtual myfunc in base. You could make it pure virtual if you intend for base to be an abstract base class, i.e one that cannot be instantiated and acts as an interface:
class base
{
public:
virtual void myfunc() = 0; // pure virtual method
};
If you ant to be able to instantiate base objects then you would have to provide an implementation for myfunc:
class base
{
public:
virtual void myfunc() {}; // virtual method with empty implementation
};
There is no other clean way to do this if you want to access the function from a pointer to a base class. The safetest option is to use a dynamic_cast
base* pbase = new derived;
....
derived* pderived = dynamic_cast<derived*>(pbase);
if (derived) {
// do something
} else {
// error
}
To use the base class pointer, you must change the base class definition to be:
class base
{
public:
virtual void myFunc() { }
};
I see no other way around it. Sorry.
You could add it as a member of base and make it a virtual or pure virtual function. If using this route however, you should also add a virtual destructor in the base class to allow successful destruction of inherited objects.
class base
{
public:
virtual ~base(){};
virtual void myFunc() = 0;
};
class derived : public base
{
public:
derived() {}
void myFunc() { cout << "My derived function" << std::endl; }
};