473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Size of an inherited class ?

Dear all,

I fear this is a question which has been asked zillions of
times. However, it seems to me to be such a legitimate feature that I
can't imagine it is impossible.

How can one know the size of an object in a "virtual" manner ?

For instance, how can I write a virtual function print_size(); which
prints the size of *this, and which will print the correct value with
an object of an inherited class ?

Thanks in advance for any hint,

--
François Fleuret http://cvlab.epfl.ch/~fleuret
Mar 13 '06 #1
8 2556
François Fleuret wrote:
Dear all,

I fear this is a question which has been asked zillions of
times. However, it seems to me to be such a legitimate feature that I
can't imagine it is impossible.

How can one know the size of an object in a "virtual" manner ?

For instance, how can I write a virtual function print_size(); which
prints the size of *this, and which will print the correct value with
an object of an inherited class ?


You can't, because you can't do anything useful with it. sizeof() is
used to allocate memory for new objects (before you can even call
print_size()) or memcpy existing objects (which you can't do if it has
virtual functions).

What legitimate use did you imagine?

HTH,
Michiel Salters

Mar 13 '06 #2
"François Fleuret" <fr************ **@epfl.ch> wrote in message
news:87******** ****@fleuret.ho meunix.org
Dear all,

I fear this is a question which has been asked zillions of
times. However, it seems to me to be such a legitimate feature that I
can't imagine it is impossible.

How can one know the size of an object in a "virtual" manner ?

For instance, how can I write a virtual function print_size(); which
prints the size of *this, and which will print the correct value with
an object of an inherited class ?

Thanks in advance for any hint,

--
François Fleuret
http://cvlab.epfl.ch/~fleuret


This seems to work

#include <iostream>
using namespace std;

struct Base
{
int b;
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};
struct Derived : Base
{
int array[4];
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};

int main(int argc, char* argv[])
{
Base b;
Derived d;

Base * pb1 = &b;
Base * pb2 = &d;

pb1->Print_Size() ;
pb2->Print_Size() ;
return 0;
}
--
John Carson
Mar 13 '06 #3

Mi************* @tomtom.com skrev:
François Fleuret wrote:
Dear all,

I fear this is a question which has been asked zillions of
times. However, it seems to me to be such a legitimate feature that I
can't imagine it is impossible.

How can one know the size of an object in a "virtual" manner ?

For instance, how can I write a virtual function print_size(); which
prints the size of *this, and which will print the correct value with
an object of an inherited class ?
You can't, because you can't do anything useful with it. sizeof() is
used to allocate memory for new objects (before you can even call
print_size()) or memcpy existing objects (which you can't do if it has
virtual functions).

What legitimate use did you imagine?


It is difficult to find a use for that size, but perhaps some sort of
clone:

class base
{
public:
virtual ~base() = 0;
virtual int size() = 0;
virtual void clone(void* adress) = 0;
};

void silly(base* b)
{
void* clonemem = malloc(b->size());
b->clone(clonemem );
}
/Peter HTH,
Michiel Salters


Mar 13 '06 #4
Dear John,

You wrote on 13 Mar 2006 12:51:47 MET:
This seems to work

#include <iostream>
using namespace std;

struct Base
{
int b;
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};
struct Derived : Base
{
int array[4];
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};

int main(int argc, char* argv[])
{
Base b;
Derived d;

Base * pb1 = &b;
Base * pb2 = &d;

pb1->Print_Size() ;
pb2->Print_Size() ;
return 0;
}


Yeah, but since sizeof used the static type of the class, you have to
re-write the method Print_Size -- with the exact same code -- in the
derived class.

I was wondering if there was a way to access the information about the
class size from its virtual table.

Cheers,

--
François Fleuret http://cvlab.epfl.ch/~fleuret
Mar 13 '06 #5
François Fleuret wrote:
Dear John,

You wrote on 13 Mar 2006 12:51:47 MET:
This seems to work

#include <iostream>
using namespace std;

struct Base
{
int b;
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};
struct Derived : Base
{
int array[4];
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};

int main(int argc, char* argv[])
{
Base b;
Derived d;

Base * pb1 = &b;
Base * pb2 = &d;

pb1->Print_Size() ;
pb2->Print_Size() ;
return 0;
}
Yeah, but since sizeof used the static type of the class, you have to
re-write the method Print_Size -- with the exact same code -- in the
derived class.


That's right. You could use templates to avoid that:

template<typena me T>
struct Base
{
int b;
virtual void Print_Size() const
{
cout << sizeof(T) << endl;
}
};

struct Derived : Base<Derived>
{
//...
};
I was wondering if there was a way to access the information about the
class size from its virtual table.


No. The C++ standard doesn't even define the term "virtual table" or
anything similar.

Mar 13 '06 #6
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:dv******** *****@news.t-online.com
François Fleuret wrote:
Dear John,

You wrote on 13 Mar 2006 12:51:47 MET:
This seems to work

#include <iostream>
using namespace std;

struct Base
{
int b;
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};
struct Derived : Base
{
int array[4];
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};

int main(int argc, char* argv[])
{
Base b;
Derived d;

Base * pb1 = &b;
Base * pb2 = &d;

pb1->Print_Size() ;
pb2->Print_Size() ;
return 0;
}


Yeah, but since sizeof used the static type of the class, you have to
re-write the method Print_Size -- with the exact same code -- in the
derived class.


That's right. You could use templates to avoid that:

template<typena me T>
struct Base
{
int b;
virtual void Print_Size() const
{
cout << sizeof(T) << endl;
}
};

struct Derived : Base<Derived>
{
//...
};

This works nicely for Derived objects. But how do you declare a Base object?

--
John Carson

Mar 13 '06 #7
John Carson wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:dv******** *****@news.t-online.com
François Fleuret wrote:
Dear John,

You wrote on 13 Mar 2006 12:51:47 MET:

This seems to work

#include <iostream>
using namespace std;

struct Base
{
int b;
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};
struct Derived : Base
{
int array[4];
virtual void Print_Size()
{
cout << sizeof(*this) << endl;
}
};

int main(int argc, char* argv[])
{
Base b;
Derived d;

Base * pb1 = &b;
Base * pb2 = &d;

pb1->Print_Size() ;
pb2->Print_Size() ;
return 0;
}

Yeah, but since sizeof used the static type of the class, you have to
re-write the method Print_Size -- with the exact same code -- in the
derived class.


That's right. You could use templates to avoid that:

template<typena me T>
struct Base
{
int b;
virtual void Print_Size() const
{
cout << sizeof(T) << endl;
}
};

struct Derived : Base<Derived>
{
//...
};

This works nicely for Derived objects. But how do you declare a Base
object?


Oh, I forgot. You need a third class that is the base to all Base<> template
instances. Here is a complete and tested example:

#include <iostream>

using std::cout;
using std::endl;

struct Base
{
virtual void Print_Size() const = 0;
virtual ~Base() {}
};

template<typena me T>
struct TBase : Base
{
virtual void Print_Size() const
{
cout << sizeof(T) << endl;
}
};

struct Derived : TBase<Derived>
{
//...
int arr[32];
};

int main()
{
Base* test = new Derived;
test->Print_Size() ;
delete test;
}

Mar 14 '06 #8
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:dv******** *****@news.t-online.com
John Carson wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:dv******** *****@news.t-online.com

That's right. You could use templates to avoid that:

template<typena me T>
struct Base
{
int b;
virtual void Print_Size() const
{
cout << sizeof(T) << endl;
}
};

struct Derived : Base<Derived>
{
//...
};

This works nicely for Derived objects. But how do you declare a Base
object?


Oh, I forgot. You need a third class that is the base to all Base<>
template instances. Here is a complete and tested example:

#include <iostream>

using std::cout;
using std::endl;

struct Base
{
virtual void Print_Size() const = 0;
virtual ~Base() {}
};

template<typena me T>
struct TBase : Base
{
virtual void Print_Size() const
{
cout << sizeof(T) << endl;
}
};

struct Derived : TBase<Derived>
{
//...
int arr[32];
};

int main()
{
Base* test = new Derived;
test->Print_Size() ;
delete test;
}

You still haven't used a Base object. I am not sure in what way this version
improves on the earlier one.

Further, what happens with your scheme if you need a second level of
derivation: a Derived2 that derives from Derived?

--
John Carson
Mar 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
4514
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I...
4
4419
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with one vtbl ptr BUt when I derive the class B from class A (both have 1 virtual function each ) the size remains still as 4 bytes . class A ...
8
3059
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this? this.GetType().GetMember("panel1").SetValue(xx).Left = 44;
8
6679
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and contains a few controls anchored to the sides of the form. "frmChild" inherits from "frmBase"... and the controls appear on the inherited form as...
3
5464
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile. (You must have a <% Register %> so it will see it) You will get TWO warnings per class like:
14
2614
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the...
26
7170
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm and was thus wondering whether I'd be better off tracking the size "manually" or whether that'd be pointless. Thanks,
12
2067
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me with the following: Can this create any problems with GNU g++ compilation, linking etc? What would be the impact to the performance of the system?...
12
500
by: shapper | last post by:
Hello, I have something has follows: <div> ... <table> ... </table>
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.