Hi,
Please have a look at this very small piece of code...
class Base1{
public:
Base1():x(5){}
int x;
};
class Base2{
public:
Base2():y(7){}
int y;
};
class Base3{
public:
Base3():z(9){}
int z;
};
class Derived:public Base1, public Base2,public Base3{
public:
Derived():w(12){}
int w;
};
int main(void)
{
Derived* p_d = new Derived;
Base1* p_b1 = dynamic_cast<Base1*(p_d);
Base2* p_b2 = dynamic_cast<Base2*(p_d);
Base3* p_b3 = dynamic_cast<Base3*(p_d);
Derived* p_d2 = static_cast<Derived*(p_b2);
delete p_d;
return 0;
}
The code just has 3 base classes, and one class that derives from all
three of them.
Now, when I cast from Derived to any of the bases, I understand that
the pointer value may change because otherwise the new pointer will
not be able to figure out where the virtual table entry is, right?
My question, however, is, how does performing the static cast from
Base2* to Derived* "know" that it should change the address back? The
code surprisingly works fine (when you cast a Base2* to a Derived* it
automatically decrements the pointer by 4 bytes.)
Is this behavior part of the C++ language? I was curious about how it
works inside.
Thank you very much in advance 3 2192
On Jun 2, 7:40 pm, responsible <msd...@gmail.comwrote:
Hi,
Please have a look at this very small piece of code...
class Base1{
public:
Base1():x(5){}
int x;
};
class Base2{
public:
Base2():y(7){}
int y;
};
class Base3{
public:
Base3():z(9){}
int z;
};
class Derived:public Base1, public Base2,public Base3{
public:
Derived():w(12){}
int w;
};
int main(void)
{
Derived* p_d = new Derived;
Base1* p_b1 = dynamic_cast<Base1*(p_d);
Base2* p_b2 = dynamic_cast<Base2*(p_d);
Base3* p_b3 = dynamic_cast<Base3*(p_d);
Derived* p_d2 = static_cast<Derived*(p_b2);
delete p_d;
return 0;
}
The code just has 3 base classes, and one class that derives from all
three of them.
Now, when I cast from Derived to any of the bases, I understand that
the pointer value may change because otherwise the new pointer will
not be able to figure out where the virtual table entry is, right?
My question, however, is, how does performing the static cast from
Base2* to Derived* "know" that it should change the address back? The
code surprisingly works fine (when you cast a Base2* to a Derived* it
automatically decrements the pointer by 4 bytes.)
Is this behavior part of the C++ language? I was curious about how it
works inside.
Thank you very much in advance
Here is a good article that you might find useful. http://www.ddj.com/dept/cpp/184402074
responsible wrote:
Hi,
Please have a look at this very small piece of code...
class Base1{
public:
Base1():x(5){}
int x;
};
class Base2{
public:
Base2():y(7){}
int y;
};
class Base3{
public:
Base3():z(9){}
int z;
};
class Derived:public Base1, public Base2,public Base3{
public:
Derived():w(12){}
int w;
};
int main(void)
{
Derived* p_d = new Derived;
Base1* p_b1 = dynamic_cast<Base1*(p_d);
Base2* p_b2 = dynamic_cast<Base2*(p_d);
Base3* p_b3 = dynamic_cast<Base3*(p_d);
There is no need to cast from derived class to a base class.
Derived* p_d2 = static_cast<Derived*(p_b2);
delete p_d;
return 0;
}
The code just has 3 base classes, and one class that derives from all
three of them.
Now, when I cast from Derived to any of the bases, I understand that
the pointer value may change because otherwise the new pointer will
not be able to figure out where the virtual table entry is, right?
Maybe. It's implementation dependant.
My question, however, is, how does performing the static cast from
Base2* to Derived* "know" that it should change the address back?
Because it knows where the Base2 part is within a Derived, since it has
definitions of both classes.
The code surprisingly works fine (when you cast a Base2* to a Derived* it
automatically decrements the pointer by 4 bytes.)
Is this behavior part of the C++ language?
If by "this behavior", you mean that a static_cast from pointer to base to
pointer to derived works, yes.
responsible wrote:
My question, however, is, how does performing the static cast from
Base2* to Derived* "know" that it should change the address back?
Simple: The compiler sees the definition of Base2 and the one of
Derived, and it sees that Derived is (multiple-)inherited from Base2.
Thus it's rather trivial for it to make the proper conversion when
casting. (Of course since you are using a static_cast the compiler
doesn't perform any sanity checks and simply trusts you on that you
know what you are doing and that there really is an object of type
Derived behind the pointer. If there isn't you probably get a crash
or other misbehavior.) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Paul |
last post by:
In real life situation, do we ever come across a situation where we
would need two base objects in an object.
A snippet is worth 1000 words (: so...
class Base
{
};
class Derived1:public Base...
|
by: Matthew Louden |
last post by:
I want to know why C# doesnt support multiple inheritance? But why we can
inherit multiple interfaces instead? I know this is the rule, but I dont
understand why. Can anyone give me some concrete...
|
by: Gaetan |
last post by:
hi
i have 2 classes A1 and A2 implementing a problem with 2 different
ways
i also have 2 other classes X1 and X2 implementing an other problem
i need classes that provide A1+X1 methods,...
|
by: Shawnk |
last post by:
Some Sr. colleges and I have had an on going discussion relative to when and
if
C# will ever support 'true' multiple inheritance.
Relevant to this, I wanted to query the C# community (the...
|
by: devicerandom |
last post by:
Hi,
I am currently using the Cmd module for a mixed cli+gui application. I
am starting to refactor my code and it would be highly desirable if
many commands could be built as simple plugins.
...
| |
by: Larry Smith |
last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that:
"Under the CLR object model, only public single inheritance is supported".
Does this mean that no .NET class can ever...
|
by: raylopez99 |
last post by:
Well, contrary to the implication in my 2000 textbook on C# (public
beta version), C# does allow multiple inheritance, so long as it's
serially chained as follows:
class derived02 : derived01
{...
|
by: Enrico |
last post by:
Hi there,
I have the following situation (I tryed to minimize the code to concentrate
on the issue):
def __getattr__(self, name):
print 'A.__getattr__'
if name == 'a': return 1
raise...
|
by: Stephen Horne |
last post by:
I've been using Visual C++ 2003 for some time, and recently started
working on making my code compile in GCC and MinGW. I hit on lots of
unexpected problems which boil down to the same template...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |