void* convert to base class and invoke virtual function [duplicate] - c++

This question already has answers here:
multiple inheritance: unexpected result after cast from void * to 2nd base class
(3 answers)
Closed 8 years ago.
I have some codes like this:
#include <iostream>
using namespace std;
//base class
class Base
{
public:
Base(){}
virtual void f()
{
cout<<"father.f()"<<endl;
}
virtual ~Base(){}
};
//a empty class
class Base1
{
public:
Base1(){}
virtual ~Base1()
{
cout<<"~base1"<<endl;
}
};
//the child class of the base class
class Child :public Base
{
public:
Child(){}
virtual ~Child(){}
};
//the grandchild class of the base class,and child class of the base1 class
class Grand :public Base1,public Child
{
public:
Grand(){}
virtual ~Grand(){}
//overwrite base's f()
void f()
{
cout<<"grand.f"<<endl;
}
};
int main()
{
void *v = new Grand();
Base *b = (Base*)(v);
//i think it print "father.f()",but it print"~base1"
b->f();
return 0;
}
I run these code in code::blocks(version13.12),but it print "~base1",I don't konw why it don't print "father.f()"。I think the v point to the address of the grand's virtual table,so when i convert v to base and invoke the f function,then b invoke the first virtual function in the virtual table.
I do not know i'm right or wrong,can u tell me.Thank all of!

Because with multiple inheritance you can't cast directly to a upper class without passing by the child class. This because a void* doesn't contain any information about the layout in memory of the various classes that form your final Grand* instance.
To fix this you need indeed to cast it to it's runtime type:
Base *b = (Base*)(Grand*)v;
b->f();
Think about the fact that the layout could have Base in a different position. Think about another
class Grand2 : public Something, public Else, public Base1, public Child {
..
}
Now, where is Base located inside the Grand2 structure? In a different position compared to Grand. How could the compiler know the correct offset starting from just a pointer to a non specified class (which could be a Grand or a Grand2)?

Related

extend a class using a host class but not inheritance

I would like to create a thin host class of a specific class to extend a specific functionality but not necessarily inheriting it. For example, class Base is described as:
class Base
{
public:
void func_1();
void func_2();
...
void func_n();
};
class Base1 will inherit Base but store a pointer of Base
class Base1 : public Base
{
public:
void func_1()
{
mpBase->func_1();
// do something else
}
private:
Base* mpBase;
}
Is there a way to automatically invoke mpBase->func_2 to func_n for Base1 because they are basically the same (n can be a large number)? Using pointer it is not done automatically. The reason I want to do it because I want to extend func_1 for all possible inherited classes of Base, without creating Base1 for every inherited classes.
Best

how to cast to "this" type from base to derived class

I want to retrieve the initial type in the base class A because the method encodeMsg() will have tasks that are related to the derived type
#include "stdafx.h"
#include <iostream>
struct base {
};
struct derive : base {
};
class A{
public:
A(){}
virtual void encode() {
encodeMsg(_bStruct);
// here I want to cast base to derive but I don't know how I tried
// static_cast<decltype(this)>(_bStruct) but it seems doesn't work
}
virtual void setStruct(base* bStruct) {
_bStruct = bStruct;
}
private:
virtual void encodeMsg(base* bStruct) = 0;
protected:
base* _bStruct;
};
class B : public A {
public:
B(){}
void encodeMsg(base* bStruct) {
derive* _dStruct = static_cast<derive*>(bStruct);
// ..
// stuff within dervied struct
_bStruct = static_cast<base*>(_dStruct); // turn back to base struct
}
};
int main() {
B b;
base* _bStruct = new derive();
b.setStruct(_bStruct);
b.encode();
return 0;
}
The question makes no sense.
In your code, the only thing you could do with the pointer, if you could create it, is access members known to be in the base class. But you can do that already without the pointer. So what purpose does the pointer serve?
If you want to one thing if it's a derived1 and something else if it's a derived2, then you need to use one piece of code for the derived1 case and one piece of code for the derived2 case.
If you want to do the same thing regardless of what derived type it is, there is no need for the cast you seek. The code in this function will be the same regardless of what the derived type is.
If you want code that only works if the derived type is some specific type, use a dynamic_cast to a pointer to that type and write the specific code. If you want code that works regardless of the derived type, use a pointer to an instance of the base class.
Update:
To access members not in base but in a derived1, use this code:
virtual void encode() {
derived1* d1 = dynamic_cast<derived1*>(bStruct);
if (d1 != nullptr)
{
// here you can access d1->whatever
}
}

Virtual table in polymorphism [duplicate]

This question already has answers here:
Does every object of virtual class have a pointer to vtable?
(9 answers)
Closed 6 years ago.
I've read a lot of posts and everyone says, that virtual table is per class, not per object and object only has _vtpr pointer to shared vtable. But please consider this example:
class Base
{
public:
virtual void func1(void) {}
virtual void func2(void) {}
private:
int dummy;
};
class Der1 : public Base
{
public:
virtual void func1(void) {}
private:
int dummy;
};
class Der2 : public Base
{
public:
virtual void func2(void) {}
private:
int dummy;
};
int main(void)
{
Base * obj1 = new Der1;
Base * obj2 = new Der2;
}
Does obj1 and obj2 relates to that one unique Base class vtable? I believe the answer is no, but can you please explain? And if both of these objects do relate to the same vtable, how it is determined which methods to be called? For example, obj1->func1 reference is different than obj2->func1.
UPDATE:
What operations are executed when doing Base * obj1 = new Der1;? Can someone write a pseudo code for these actions?
I hope the following illustration will help. Derived classes just copy the virtual table of base class, but they may modify the appropriate entry. The red part shows that given class has modified the entry in v-table. So, when instance of derived class is created, the modified table (specific to that class) is taken into consideration.
It is up to the compiler how duplicity of base class is handled (if base class has large number of functions - would derived have copy of entire virtual function table, and would modify the entries?).
IMO, compiler will simply copy entire table for each derived class to keep things simple. Searching appropriate method to call then becomes simple.

What exactly does runtime polymorphism mean?

I'm slightly confused about runtime polymorphism. Correct me if I am wrong, but to my knowledge, runtime polymorphism means that function definitions will get resolved at runtime.
Take this example:
class a
{
a();
~a();
void baseclass();
}
class b: class a
{
b();
~b();
void derivedclass1();
}
class c: class a
{
c();
~c();
void derivedclass2();
}
Calling methodology:
b derived1;
a *baseptr = &derived1; //here base pointer knows that i'm pointing to derived class b.
baseptr->derivedclass1();
In the above calling methodology, the base class knows that it's pointing to derived class b.
So where does the ambiguity exist?
In what cases will the function definitions get resolved at runtime?
This code, at run time, calls the correct version of f() depending on the type of object (A or B) that was actually created - no "ambiguity". The type cannot be known at compile-time, because it is selected randomly at run-time.
struct A {
virtual ~A() {}
virtual void f() {}
};
struct B : public A {
virtual void f() {}
};
int main() {
A * a = 0;
if ( rand() % 2 ) {
a = new A;
}
else {
a = new B;
}
a->f(); // calls correct f()
delete a;
}
There is no ambiguity exists in the example provided.
If the base class has the same function name as the derived class, and if you call in the way you specified, it will call the base class's function instead of the derived class one.
In such cases, you can use the virtual keyword, to ensure that the function gets called from the object that it is currently being pointed. It is resolved during the run time.
Here you can find more explanation..
Turn this
void baseclass();
to
virtual void baseclass();
Override this in your Derived classes b and c. Then
b *derived1 = new derived1 ();
a *baseptr = derived1; //base pointer pointing to derived class b.
baseptr->baseclass();
will invoke derived1 definition, expressing run time polymorphism. And do remember about making your destructor virtual in Base. Some basic reading material for polymorphism
Runtime means that exact method will be known only at run time. Consider this example:
class BaseClass
{
public:
virtual void method() {...};
};
class DerivedClassA : public BaseClass
{
virtual void method() {...};
};
class DerivedClassB : public BaseClass
{
virtual void method() {...};
};
void func(BaseClass* a)
{
a->method();
}
When you implement your ::func() you don't know exactly type of instance pointed by BaseClass* a. It might be DerivedClassA or DerivedClassB instance etc.
You should realize, that runtime polymorphism requires special support from language (and maybe some overhead for calling "virtual" functions). In C++ you "request" for dynamic polymorphism by declaring methods of base class "virtual" and using public inheritance.
You need to have some useful business method declared in the base and in each derived class. Then you have code such as
a->someMethod();
Now the a pointer might point to an instance of any of the derived classes, and so the type of what a is pointing to must determine which someMethod() is called.
Lets have an experiment
#include <iostream>
using namespace std;
class aBaseClass
{
public:
void testFunction(){cout<<"hello base";}///Not declared as virtual!!!!
};
class aDerivedClass:public aBaseClass
{
public:
void testFunction(){cout<<"hello derived one";}
};
class anotherDerivedClass:public aDerivedClass
{
public:
void testFunction(){cout<<"hello derived two";}
};
int main()
{
aBaseClass *aBaseClassPointer;
aBaseClassPointer=new aDerivedClass;
aBaseClassPointer->testFunction();
}
The above code does not support run time polymorphism. Lets run and analyze it.
The output is
hello base
Just change the line void testFunction(){cout<<"hello base";} to virtual void testFunction(){cout<<"hello base";} in aBaseClass. Run and analyze it. We see that runtime polymorphism is achieved. The calling of appropriate function is determined at run time.
Again change the line aBaseClassPointer=new aDerivedClass to aBaseClassPointer=new anotherDerivedClass in main function and see the output. Thus the appropriate function calling is determined at run time (when the program is running).

Casting to one class and calling function from sibling class?

I'm getting a pointer to a base class (which is actually a pointer to some derived class). Then I want to call a function on that derived class, but I don't know which one it is.
class Base
{
};
class DerivedOne : public Base
{
public:
void functionA()
{ int x = 0; }
};
class DerivedTwo : public Base
{
public:
void functionA()
{ int x = 0; }
};
int main()
{
Base* derivedTwoPtr = new DerivedTwo();
reinterpret_cast<DerivedOne*>(derivedTwoPtr)->functionA();
return 0;
}
This works as I want, but I have to say it looks rather dodgy. Is it defined behavior? If not, is there a legal way to dynamically resolve this?
Hey, don't do that. That's what virtual methods are for.
class Base
{
public:
virtual void functionA()=0;
};
class DerivedOne : public Base
{
public:
virtual void functionA()
{ int x = 0; }
};
class DerivedTwo : public Base
{
public:
virtual void functionA()
{ int x = 0; }
};
int main()
{
Base* derivedTwoPtr = new DerivedTwo();
derivedTwoPtr->functionA();
return 0;
}
Just use virtual functions. That's what they are intended for. Your base class should look like
class Base
{
virtual void functionA() = 0;
};
where the = 0 bit is optional. If present the virtual function is known as a pure virtual function and enforces each subclass of Base to implement the function.
Now if you call functionA through a Base pointer you will get the method appropriate to whichever subclass the pointer really points to.
is there a legal way to dynamically
resolve this?
dynamic_cast can be used to cast to a specific derived class and invoke derived class methods. But in your case the best would be to provide a virtual method in Base class and provide different implementation for the virtual method in derived classes.
You basically answered your own question here:
Casting to one class and calling
function from sibling class?
This works as I want, but I have to
say it looks rather dodgy. Is it
defined behavior? If not, is there a
legal way to dynamically resolve this?
In short:
if (DerivedOne* one=dynamic_cast<DerivedOne*>(BasePtr))
one->functionA();
else if (DerivedTwo* two=dynamic_cast<DerivedTwo*>(BasePtr))
two->functionA();
But yeah, like vava said, don't do that.