14

I understand that a class will have a VTABLE, if it contains at-least one virtual function. I would like to see the contents of the VTABLE. Is there a way to display it ?

Specifically, is there an option in gcc to display the VTABLE of a class?

2 Answers 2

28

If the input file is say layout.cpp, the command gcc -fdump-class-hierarchy layout.cpp will produce a file layout.cpp.class. This file will display the VTABLE along with some other useful information.

4
  • Are you sure we should use gcc or we should use g++ for c++ Commented Jan 25, 2017 at 16:33
  • Its g++ not gcc.
    – sagar
    Commented Dec 5, 2017 at 7:19
  • 5
    "Since GCC version 8.0 at least these two options were replaced: -fdump-class-hierarchy is now -fdump-lang-class -fdump-translation-unit ---> -fdump-lang-raw"
    – user362515
    Commented May 9, 2020 at 11:01
  • @user362515: Do you know if these options are still available in gcc version 9.0?
    – fabian
    Commented Feb 1, 2021 at 18:50
-1

Assuming it will really have a v-table (this is compiler dependent, and is not required or dictated by the standard) - you can inspect it with your debugger.

Why would you do that? If you're relying on it for anything other than curiosity - then don't.

2
  • Yes out of curiosity. Is there an option in gcc to display the vtable? Commented Jan 22, 2012 at 7:08
  • @LinuxPenseur you must mean gdb. I don't know. I know that on Visual Studio I saw it appearing once in a while when I was inspecting classes. I guess it depends on what symbol gcc attaches to it and whether you can find that symbol.
    – littleadv
    Commented Jan 22, 2012 at 8:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.