So, I tried my hand at a few things. First, I wanted to check some properties of the mythical vtable. So what I do is I create a toy object by creating a file
test.cpp
and a header Object.hpp
. The header is:
#include < iostream >
using namespace std;
class Object
{
public:
Object() { }
~Object() { }
int getAddress()
{
return (int)(this);
}
void address()
{
cout << "Object @" << getAddress() << endl;
}
void stat()
{
address();
cout << "Size of object: " << sizeof(this) << endl;
}
};
And then the test.cpp
file I don't change that much:
#include "Object.hpp"
int main()
{
Object* o = new Object();
o->stat();
return 0;
}
The number of lines of assembly for this, by using the command:
$ g++ -S test.cpp
Then opening test.s
is 376 lines. This is the control.
Spice Things Up
The next thing I try is to make all the methods and constructors/destructors virtual. I don't know how, if at all, this would change anything. Mind you, I have not taken a formal course on C++ so I don't know what happens if I change anything.
I then found out, by means of a compiler error, that I cannot make constructors virtual. I decided to make the destructor non-virtual also.
The resulting assembly file is 414 lines long. So adding the virtual qualifier bloats the program.
I assume that this is because of the vtable in some manner, but I do not know for certain.
I am ashamed to admit it, but I am going to brush up on the vtable with Wikipedia. I'll probably write about it next...or else I'll write about object oriented C.
No comments:
Post a Comment