Board index » cppbuilder » Re: How does this code behave?

Re: How does this code behave?


2007-10-09 03:13:21 AM
cppbuilder108
"Hendrik Schober" < XXXX@XXXXX.COM >writes:
Quote
I have no idea what your "VP" refers to.
My guess is vtbl-pointer.
And I agree, the code formatting is hard to read.
--
Chris (TeamB);
 
 

Re:Re: How does this code behave?

Alex Bakaev [TeamB] wrote:
Quote
And that's what (at least on the surface) seems strange. If I'm not
mistaken, language like Eiffel don't do this, i.e. you can provide two
identically named virtual functions in the derived for each of the bases.

.a
Well, as I just found out, the way C++/CLI does the renaming is coming
directly from Eiffel :) I work with one of the guys who directly
contributed the feature t Net 2.0 :)
 

Re:Re: How does this code behave?

"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
I'm not sure I understand how this code should behave:

struct Base1 { virtual int registerListener() = 0; };
struct Base2 { virtual int registerListener() = 0; };

class Derived : public Base1, public Base2 {
public:
int registerListener()
[...]
The code is legal, and registerListener in Derived overrides
the Base1 function as well as the Base2 function. In fact I'm
told there's an example of this in TC++PL.
If you want to only override the Base1 function, you have to
introduce an intermediate class between Base2 and Derived.
 

{smallsort}

Re:Re: How does this code behave?

"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
I'm not sure I understand how this code should behave:

struct Base1 { virtual int registerListener() = 0; };
struct Base2 { virtual int registerListener() = 0; };

class Derived : public Base1, public Base2 {
public:
int registerListener()
[...]
The code is legal, and registerListener in Derived overrides
the Base1 function as well as the Base2 function. In fact I'm
told there's an example of this in TC++PL.
If you want to only override one of the functions, you have to
introduce an intermediate class.
 

Re:Re: How does this code behave?

Sorry but tabs were ignored when I pasted the code.
VP is vtbl-pointer.:)
I tried to say that then an object of a derived class is created it has the
same number of pointers to vtbls as the number of abstract base classes
from which it was derived.
So if a class c4 is derived from three abstract classes c1, c2, c3 then its
sizeof( c4 ) (it has not any data member only one virtual function) will be
equal to size of a pointer multiplyed by number of abstract classes.
Vladimir Grigoriev
"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
"Hendrik Schober" < XXXX@XXXXX.COM >writes:

>I have no idea what your "VP" refers to.

My guess is vtbl-pointer.

And I agree, the code formatting is hard to read.

--
Chris (TeamB);
 

Re:Re: How does this code behave?

In your example classes A and B is not abstract classes.
Vladimir Grigoriev
"Hendrik Schober" < XXXX@XXXXX.COM >wrote in message
Quote
Vladimir Grigoriev < XXXX@XXXXX.COM >wrote:
>For information. If to run the code
>[code snipped]
>we get as result the following
>
>sizeof( A ) = 4 sizeof( B ) = 4 sizeof( C ) = 8
>
>So derived structure C changes all two entries in virtual table with it
>function f (I think so). [...]

I don't think so. Look at this:

#include <iostream>

struct A {
virtual int f() const {return 0;}
};


struct B {
virtual int g() const {return 0;}
};

struct C: public A, B {
};


int main()
{
std::cout << "sizeof(A) = " << sizeof(A)
<< "\nsizeof(B) = " << sizeof(B)
<< "\nsizeof(C) = " << sizeof(C)
<< std::endl;
return 0;
}


For me, using VC8, this produces the same result.

>Vladimir Grigoriev

Schobi

--
XXXX@XXXXX.COM is never read
I'm HSchober at gmx dot de
"A patched buffer overflow doesn't mean that there's one less way
attackers
can get into your system; it means that your design process was so lousy
that it permitted buffer overflows, and there are probably thousands more
lurking in your code."
Bruce Schneier

 

Re:Re: How does this code behave?

"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote:
Quote
I tried to say that then an object of a derived class is created it has the
same number of pointers to vtbls as the number of abstract base classes
from which it was derived.
Ah, now that's where you were wrong, you see. It only needs a pointer to
one table, which can then contain as many pointers as is required.
(It *can* have multiple pointers, it only *needs* one. Table layouts
with multiple base classes gets complicated, and casting can then
involve pointer arithmetic.)
Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK
 

Re:Re: How does this code behave?

Alan Bellingham < XXXX@XXXXX.COM >wrote:
Quote
Ah, now that's where you were wrong, you see. It only needs a pointer to
one table, which can then contain as many pointers as is required.
On consideration, I'm not entirely sure of this - so please consider
this retracted.
Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK
 

Re:Re: How does this code behave?

The results show ( VC++ 2005 EE) that if a class (with only one virtual
function and nothing more) is derived from four abstract classes then its
sizeof == 16.
If a class is derived from three abstract classes then its sizeof == 12,
and so on.
Vladimir Grigoriev
"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
Quote
Alan Bellingham < XXXX@XXXXX.COM >wrote:

>Ah, now that's where you were wrong, you see. It only needs a pointer to
>one table, which can then contain as many pointers as is required.

On consideration, I'm not entirely sure of this - so please consider
this retracted.

Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK
 

Re:Re: How does this code behave?

"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote:
Quote
In your example classes A and B is not abstract classes.
[snip]
Quote
>struct A {
>virtual int f() const {return 0;}
>};
>
>
>struct B {
>virtual int g() const {return 0;}
>};
[snip]
They look like it from here. Both contain virtual functions.
Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK
 

Re:Re: How does this code behave?

"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
Quote
"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote:

>In your example classes A and B is not abstract classes.

[snip]

>>struct A {
>>virtual int f() const {return 0;}
>>};
>>
>>
>>struct B {
>>virtual int g() const {return 0;}
>>};

[snip]

They look like it from here. Both contain virtual functions.
This may be a matter of semantics but AFAIK an abstract class
is one that contains at least one PURE virtual function, no?
And, as such, cannot be instantiated. Neither of these is pure virtual.
 

Re:Re: How does this code behave?

"Duane Hebert" < XXXX@XXXXX.COM >wrote in message
Quote

"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote:
>
>>In your example classes A and B is not abstract classes.
>
>[snip]
>
>>>struct A {
>>>virtual int f() const {return 0;}
>>>};
>>>
>>>
>>>struct B {
>>>virtual int g() const {return 0;}
>>>};
>
>[snip]
>
>They look like it from here. Both contain virtual functions.

This may be a matter of semantics but AFAIK an abstract class
is one that contains at least one PURE virtual function, no?
And, as such, cannot be instantiated. Neither of these is pure virtual.
Unless the struct A and B here are not the class A and B referred
to in the comment. (top posting sucks BTW)
 

Re:Re: How does this code behave?

Old Wolf wrote:
Quote

The code is legal, and registerListener in Derived overrides
the Base1 function as well as the Base2 function. In fact I'm
told there's an example of this in TC++PL.
Yes, thank you. That was surprising to me.
.a
 

Re:Re: How does this code behave?

"Duane Hebert" < XXXX@XXXXX.COM >wrote:
Quote
This may be a matter of semantics but AFAIK an abstract class
is one that contains at least one PURE virtual function, no?
And, as such, cannot be instantiated. Neither of these is pure virtual.
OK, blame the fact I've been having to code Java for the last few weeks.
It's obviously rotting my brain away.
(And coping with top-posted originals is not my excuse.)
What I should have been worrying about was that they were both
*polymorphic* classes. Whether they also happen to be abstract or not is
rather irrelevant to vtable layout, since the effect of pure virtual
functions is to prevent instantiation of the class in which they exist,
not to change inheritance.
Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK
 

Re:Re: How does this code behave?

"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
Quote
"Duane Hebert" < XXXX@XXXXX.COM >wrote:

>This may be a matter of semantics but AFAIK an abstract class
>is one that contains at least one PURE virtual function, no?
>And, as such, cannot be instantiated. Neither of these is pure virtual.

OK, blame the fact I've been having to code Java for the last few weeks.
It's obviously rotting my brain away.
At least it will do it slowly.
Quote
(And coping with top-posted originals is not my excuse.)
I thought it was my bad since I know that you know better.
Quote
What I should have been worrying about was that they were both
*polymorphic* classes. Whether they also happen to be abstract or not is
rather irrelevant to vtable layout, since the effect of pure virtual
functions is to prevent instantiation of the class in which they exist,
not to change inheritance.
Sure. But the initial question had to do with two abstract
base classes with the same pure virtual function (at least
by name) and how the derived class handled it. I'm not
sure what the example without abstract base classes was
getting at.