Re: virtual inheritance and Glib::Interface
- From: "Jonathon Jongsma" <jonathon jongsma gmail com>
- To: "Murray Cumming" <murrayc murrayc com>
- Cc: Chris Vine <chris cvine freeserve co uk>, gtkmm-list gnome org
- Subject: Re: virtual inheritance and Glib::Interface
- Date: Fri, 7 Apr 2006 13:31:24 -0500
On 4/7/06, Murray Cumming <murrayc murrayc com> wrote:
> Another reason that occurs to me: We generally use virtual inheritance
> to share class data (member variables), but there are no member
> variables in Glib::Interface - just in the base classes which are
> shared. Hopefully we don't lose too many bytes just by having the the
> class instance itself.
True. There's probably not a big penalty at all here. Out of
curiosity, I just did a couple very simplistic tests and thought the
results were sort of interesting. Here's the first test program:
-----------------------------------------------
#include <iostream>
using namespace std;
#define VIRTUAL // virtual
class base
{
public:
base(void) { cout << "base created" << endl;}
void baseFunc(void) {}
};
class derived1 : VIRTUAL public base
{
public:
void derived1Func(void) {}
};
class derived2 : VIRTUAL public base
{
public:
void derived2Func(void) {}
};
class derived3 : VIRTUAL public base
{
public:
void derived3Func(void) {}
};
class derived4 : VIRTUAL public base
{
public:
void derived4Func(void) {}
};
class mostderived :
public derived1,
public derived2,
public derived3,
public derived4
{
public:
void mostderivedFunc(void) {}
};
int main()
{
mostderived d;
cout << "sizeof d is " << sizeof(d) << endl;
return 0;
}
-----------------------------------------------
When VIRTUAL is not defined I get the following output:
base created
base created
base created
base created
sizeof d is 4
When VIRTUAL is defined as 'virtual', I get the following:
base created
sizeof d is 16
This seems to imply that using virtual inheritance actually
/increases/ the memory usage when the inherited classes don't have any
data members. On the other hand, when I added a sub-base class (think
of it as an equivalent to Glib::ObjectBase, whereas base is the
equivalent to Glib::Interface) that does have data members (as
Glib::ObjectBase does) but is inherited virtually from base, the
memory usage is exactly the same.
Test Code:
-----------------------------------------------
#include <iostream>
using namespace std;
#define VIRTUAL virtual
struct myStruct
{
int something;
};
class basebase
{
public:
basebase() { cout << "basebase created" << endl; }
private:
bool x;
const char* y;
myStruct* cobj;
};
class base : virtual public basebase
{
public:
base(void) { cout << "base created" << endl;}
void baseFunc(void) {}
};
class derived1 : VIRTUAL public base
{
public:
void derived1Func(void) {}
};
class derived2 : VIRTUAL public base
{
public:
void derived2Func(void) {}
};
class derived3 : VIRTUAL public base
{
public:
void derived3Func(void) {}
};
class derived4 : VIRTUAL public base
{
public:
void derived4Func(void) {}
};
class mostderived :
public derived1,
public derived2,
public derived3,
public derived4
{
public:
void mostderivedFunc(void) {}
};
int main()
{
mostderived d;
cout << "sizeof d is " << sizeof(d) << endl;
return 0;
}
-----------------------------------------------
Output when VIRTUAL is undefined:
basebase created
base created
base created
base created
base created
sizeof d is 28
Output when VIRTUAL is defined as 'virtual':
basebase created
base created
sizeof d is 28
I'm not exactly sure what to make of these results. But in any case,
it appears that there's not a lot of difference either way.
Jonner
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]