Re: I don't get it
This is a multi-part message in MIME format.
--------------070601080003030802070201
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Thomas J. Gritzan wrote:
Jo wrote:
Imho, if class C is derived from A and B, then C IS an A and a B at the
same time, conceptually.
But i'm understanding that there is a technical implementation problem
here.
In the OP you tried to get a B* from a void*. A void pointer is everything
and nothing, the compiler cannot know what type of object it points to.
OK, understood.
So avoid void*. Avoid c-style cast, too. With C++ style cast, the compiler
can warn you whats wrong using them.
OK, will take this into account.
I thought the compiler could figure that out, but i'm clearly over
estimating the compiler and/or C++.
Anyway, it's off topic to go deeper in this. Although that would be a
nice discussion.
When it's a language thing, it is on-topic.
The point was: When C is derived from A and B, then C is an A and C is a
B at the same time.
I would have been surprised if C++/the compiler would not be able to do
the up/down casts in this family, even when multiple inheritance is into
play.
(i immediately thought of a system how this would be no problem)
For a moment, i thought John said that C is not a B. But i
misunderstood. Now i understand he said that the void* p is not pointing
to the B object (in the given example) which is right because it's a void*.
If the C++ compiler can figure out the A/B/C up/down casts, then of
course i keep on being a happy C++ -er.
I know all this is pure essential.
I'm a bit amazed/ashamed that i ran into this problem (i.e. using the
void*).
I didn't know that when using a void*, you explicitly abandon all type
info, except for the source type.
Perhaps you are expecting an ordinary cast to work like a
dynamic_cast? When you write
bp2=(B*)p; // resulting bp2 is WRONG!
you are expecting the program to 'discover' that there a B object
hidden inside the object that p is pointing to?
If so google for dynamic_cast, or better still read a good book.
I understand that a dynamic_cast cannot do a base-to-derived conversion,
so that's not the solution here.
dynamic_cast can, and is exactly the right tool, to do base-to-derived
conversion. It's like static_cast with an explicit runtime type check.
I understand.
So i'm still looking for the solution for this situation
class A {
public:
virtual long GetClassID() { return(' A'); }
};
[...]
class C : public class A, public class B {
public:
virtual long GetClassID() { return(' C'); }
};
foo1(C *cp) {
...
foo2(cp);
...
}
foo2(A *ap) {
if (ap->GetClassID==' C') {
C *cp=(C*)ap; // IS THIS LEGAL and PORTABLE C++ ?
}
}
When you know that an A* points to a C object, you can static_cast it to a
C*. But why inventing your own RTTI? You can use typeid and dynamic_cast
instead.
It's indeed about having a 'little private' RTTI system.
I would like to not use RTTI because i heard about the overhead it brings.
Not sure if that is memory or cpu overhead, or both?
The application i'm working on is a multi-threaded realtime application
where part of the code runs at interrupt level.
And that code must be fast, and may not do any dynamic memory allocs!
So that's why i want to play it safe.
--------------070601080003030802070201
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
Thomas J. Gritzan wrote:<br>
<blockquote type="cite" cite="midf56p8k$1ss$1@newsreader3.netcologne.de">
<pre wrap="">Jo wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Imho, if class C is derived from A and B, then C IS an A and a B at the
same time, conceptually.
But i'm understanding that there is a technical implementation problem
here.
</pre>
</blockquote>
<pre wrap=""><!---->
In the OP you tried to get a B* from a void*. A void pointer is everything
and nothing, the compiler cannot know what type of object it points to.</pre>
</blockquote>
<br>
<tt>OK, understood.<br>
<br>
</tt>
<blockquote type="cite" cite="midf56p8k$1ss$1@newsreader3.netcologne.de">
<pre wrap="">So avoid void*. Avoid c-style cast, too. With C++ style cast, the compiler
can warn you whats wrong using them.</pre>
</blockquote>
<tt><br>
OK, will take this into account.<br>
<br>
</tt>
<blockquote type="cite" cite="midf56p8k$1ss$1@newsreader3.netcologne.de">
<blockquote type="cite">
<pre wrap="">I thought the compiler could figure that out, but i'm clearly over
estimating the compiler and/or C++.
Anyway, it's off topic to go deeper in this. Although that would be a
nice discussion.
</pre>
</blockquote>
<pre wrap=""><!---->
When it's a language thing, it is on-topic.</pre>
</blockquote>
<br>
<tt>The point was: When C is derived from A and B, then C is an A and C
is a B at the same time.<br>
<br>
I would have been surprised if C++/the compiler would not be able to do
the up/down casts in this family, even when multiple inheritance is
into play.<br>
<br>
(i immediately thought of a system how this would be no problem)<br>
<br>
For a moment, i thought John said that C is not a B. But i
misunderstood. Now i understand he said that the void* p is not
pointing to the B object (in the given example) which is right because
it's a void*.<br>
<br>
If the C++ compiler can figure out the A/B/C up/down casts, then of
course i keep on being a happy C++ -er.<br>
<br>
I know all this is pure essential.<br>
I'm a bit amazed/ashamed that i ran into this problem (i.e. using the
void*).<br>
I didn't know that when using a void*, you explicitly abandon all type
info, except for the source type.<br>
<br>
</tt>
<blockquote type="cite" cite="midf56p8k$1ss$1@newsreader3.netcologne.de">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">Perhaps you are expecting an ordinary cast to work like a
dynamic_cast? When you write
bp2=(B*)p; // resulting bp2 is WRONG!
you are expecting the program to 'discover' that there a B object
hidden inside the object that p is pointing to?
If so google for dynamic_cast, or better still read a good book.
</pre>
</blockquote>
<pre wrap="">
I understand that a dynamic_cast cannot do a base-to-derived conversion,
so that's not the solution here.
</pre>
</blockquote>
<pre wrap=""><!---->
dynamic_cast can, and is exactly the right tool, to do base-to-derived
conversion. It's like static_cast with an explicit runtime type check.</pre>
</blockquote>
<tt><br>
I understand.<br>
<br>
</tt>
<blockquote type="cite" cite="midf56p8k$1ss$1@newsreader3.netcologne.de">
<blockquote type="cite">
<pre wrap="">So i'm still looking for the solution for this situation
class A {
public:
virtual long GetClassID() { return(' A'); }
};
</pre>
</blockquote>
<pre wrap=""><!---->[...]
</pre>
<blockquote type="cite">
<pre wrap="">class C : public class A, public class B {
public:
virtual long GetClassID() { return(' C'); }
};
foo1(C *cp) {
...
foo2(cp);
...
}
foo2(A *ap) {
if (ap->GetClassID==' C') {
C *cp=(C*)ap; // IS THIS LEGAL and PORTABLE C++ ?
}
}
</pre>
</blockquote>
<pre wrap=""><!---->
When you know that an A* points to a C object, you can static_cast it to a
C*. But why inventing your own RTTI? You can use typeid and dynamic_cast
instead.</pre>
</blockquote>
<br>
<tt>It's indeed about having a 'little private' RTTI system.<br>
<br>
I would like to not use RTTI because i heard about the overhead it
brings.<br>
<br>
Not sure if that is memory or cpu overhead, or both?<br>
</tt><tt><br>
The application i'm working on is a multi-threaded realtime application
where part of the code runs at interrupt level.<br>
<br>
And that code must be fast, and may not do any dynamic memory allocs!<br>
<br>
So that's why i want to play it safe.<br>
</tt>
</body>
</html>
--------------070601080003030802070201--