473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2549
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.homeunix.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<typename 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<typename 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<typename 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<typename 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<typename 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<typename 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
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...
4
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...
8
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?...
8
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...
3
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....
14
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...
26
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...
12
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...
12
by: shapper | last post by:
Hello, I have something has follows: <div> ... <table> ... </table>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.