Topic: Does multiple inheritance use multiple vtbls in C++?


Author: AllanW@my-dejanews.com
Date: 1998/12/01
Raw View

> AllanW@my-dejanews.com wrote:
> > ... classes that are "abstract" generally don't need virtual tables...

In article <3663BC7B.4BE7E324@physik.tu-muenchen.de>,
  Christopher Eltschka <celtschk@physik.tu-muenchen.de> wrote:
> I disagree on the abstract base part.
> Consider the following:

[The example had class ABC with one abstract function, foo, and class
 X derived from ABC which overrides foo. main() constructs an X and
 then calls foo, but ABC's constructor also calls foo.]

> Here, the call of bar from the constructor gives an object whose
> dynamic type is ABC (since the derived class object constructor
> has not been entered yet). Therefore in bar, abc->foo() must
> resolve to ABC::foo(). OTOH, this cannot be hardcoded in
> bar, since the foo call of main gives an X to bar, so this time,
> abc->foo() must resolve to X::foo(). To accomplish this, ABC
> must have its own virtual table.

Yes, you're right. When ABC has any constructors that call virtual
functions, or call other functions with "this" as an argument, then
class ABC must have a vtable.

However, if ABC's constructors are simple enough, and the compiler
can determine that none of them can call any virtual functions
directly or indirectly, then it's safe to drop the virtual table.
In practice this is probably a difficult and therefore rare
optimization.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1998/12/01
Raw View
In article <73nv7s$b2s$1@nnrp1.dejanews.com>,
  mayurnaik@my-dejanews.com wrote:
>
> Hi,
> I have read that the implementation of multiple inheritance
> in C++ uses multiple virtual tables.
> 1. Is this true?
> 2. If yes, could it not be optimized to a single vtbl?

You posted this question to a newsgroup talking about C++ standards.
Believe it or not, the C++ standard doesn't even ONCE mention virtual
tables, or vtables, or vtbls. Never! It does specify some very exact
requirements, and as a practical matter virtual tables are the best
way to accomplish those requirements, but nothing prevents a compiler
writer from creating a new, novel way to accomplish the same thing.

As a practical matter, generally virtual tables must exist for all
non-abstract classes that have at least one virtual function. If you
had a class with multiple inheritance but no virtual functions, the
compiler might not have to create a virtual table.

As for the optimization -- first, virtual tables aren't usually very
big. You might not be able to tell if this optimization happened
unless you peeked at the generated object or assembly code. However,
classes that don't have any virtual functions (inherited or direct)
and classes that are "abstract" generally don't need virtual tables.
So the answer to #2 is probably "yes" depending on exactly what you
meant.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/12/01
Raw View
AllanW@my-dejanews.com wrote:

[...]

> However,
> classes that don't have any virtual functions (inherited or direct)
> and classes that are "abstract" generally don't need virtual tables.
> So the answer to #2 is probably "yes" depending on exactly what you
> meant.

I disagree on the abstract base part.
Consider the following:

class ABC
{
public:
  ABC();
  virtual ~ABC();
  virtual void foo()=0;
};

void bar(ABC* abc)
{
  abc->foo();
}

ABC::ABC()
{
  bar(this);
}

ABC::~ABC() {}

void ABC::foo()
{
  cout << "ABC" << endl;
}

class X: public ABC
{
public:
  void foo();
};

void X::foo()
{
  cout << "X" << endl;
}

int main()
{
  X x;
  bar(&x);
}

Here, the call of bar from the constructor gives an object whose
dynamic type is ABC (since the derived class object constructor
has not been entered yet). Therefore in bar, abc->foo() must
resolve to ABC::foo(). OTOH, this cannot be hardcoded in
bar, since the foo call of main gives an X to bar, so this time,
abc->foo() must resolve to X::foo(). To accomplish this, ABC
must have its own virtual table.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: mayurnaik@my-dejanews.com
Date: 1998/11/28
Raw View
Hi,
I have read that the implementation of multiple inheritance
in C++ uses multiple virtual tables.
1. Is this true?
2. If yes, could it not be optimized to a single vtbl?

Thanking you in advance,
Mayur

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1998/11/29
Raw View
mayurnaik@my-dejanews.com writes:

> I have read that the implementation of multiple inheritance
> in C++ uses multiple virtual tables.
> 1. Is this true?

The standard does not mandate this, but many compilers work this way.

> 2. If yes, could it not be optimized to a single vtbl?

Consider

struct A{
  int i;
  virtual void f();
  void h(){
    f();     //finds VMT at beginning of object, and finds f at offset 0
  }
};

struct B{
  int j;
  virtual void dummy();
  virtual void f();
  void g(){
    f();    //finds VMT at beginning of object, and finds f at offset 1
  }
};

struct C: A,B{
  int k;
  void f();
};

int main()
{
  C c;
  c.g();
  c.h();
}

Now, if c had a single vtable: at what index would you put void f()?

Martin



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]