Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with sizeof, when using it with Base class pointer

Reply
Thread Tools

Problem with sizeof, when using it with Base class pointer

 
 
Gopal-M
Guest
Posts: n/a
 
      06-01-2004
I have the problem with sizeof operator I also want to implement a
function that can return size of an object. My problem is as follows..

I have a Base class, say Base and there are many class derived from
it. At a particular point in my application I need the size of the
object pointed to by the base pointer. Eg





Class Base

{
private:

int m_nBaseData;

virtual void Display() = 0;

};



class Dev1: public Base
{
private:
int m_nDev1Data[10];
public:
void Display()
{
/// . Code..
}
};

class Dev2: public Base
{
private:
int m_nDev2Data[10];
public:
void Display()
{
/// . Code..
}
};


void main()
{
Base* p1 = new Dev1;
Base* p2 = new Dev2;

unsigned int size1 = sizeof(*p1); // LINE # 1

unsigned int size2 = sizeof(*p2); // LINE # 2
}



As the code given above I am getting size as 8 in LINE # 1 and LINE #
2, it is the size of the Base class.

In my application I need the size of the object pointed to by the
pointer. Means, if the base pointer contains the Dev1 obj then i
should some how need the size of the object pointed by Base pointer.
Can any one give me any solution for this.



Gopal.
Lambent Technologies
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-01-2004
Gopal-M wrote:
> I have the problem with sizeof operator I also want to implement a
> function that can return size of an object. My problem is as follows..
>
> I have a Base class, say Base and there are many class derived from
> it. At a particular point in my application I need the size of the
> object pointed to by the base pointer. Eg
>
>
>
>
>
> Class Base
>
> {
> private:
>
> int m_nBaseData;
>
> virtual void Display() = 0;
>
> };
>
>
>
> class Dev1: public Base
> {
> private:
> int m_nDev1Data[10];
> public:
> void Display()
> {
> /// . Code..
> }
> };
>
> class Dev2: public Base
> {
> private:
> int m_nDev2Data[10];
> public:
> void Display()
> {
> /// . Code..
> }
> };
>
>
> void main()
> {
> Base* p1 = new Dev1;
> Base* p2 = new Dev2;
>
> unsigned int size1 = sizeof(*p1); // LINE # 1
>
> unsigned int size2 = sizeof(*p2); // LINE # 2
> }
>
>
>
> As the code given above I am getting size as 8 in LINE # 1 and LINE #
> 2, it is the size of the Base class.
>
> In my application I need the size of the object pointed to by the
> pointer. Means, if the base pointer contains the Dev1 obj then i
> should some how need the size of the object pointed by Base pointer.
> Can any one give me any solution for this.


'sizeof' is a compile-time "operator". Since the result of the "size
of the actual object" depends on the size of the run-time _real_ object,
there is no way for 'sizeof' to properly solve this. The only way
you could attempt to fix this is to use a virtual function in 'Base'
that reports the size:

class Base {
...
virtual std::size_t getSize() const = 0; // pure, to force
// implementation
};

// implementation -- just in case
inline std::size_t Base::getSize() const { return sizeof(Base); }

class Derived1 : public Base {
...
std::size_t getSize() const { return sizeof(Derived1); }
};

....

Victor
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      06-01-2004
On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, http://www.velocityreviews.com/forums/(E-Mail Removed)
(Gopal-M) wrote,
>I have a Base class, say Base and there are many class derived from
>it. At a particular point in my application I need the size of the
>object pointed to by the base pointer. Eg


You should write your code so that it uses only the declared public
interface of your classes. The size of the object typically is not,
and should not be, anything for foreign code to depend upon.

 
Reply With Quote
 
Gopal-M
Guest
Posts: n/a
 
      06-02-2004
David Harmon <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, (E-Mail Removed)
> (Gopal-M) wrote,
> >I have a Base class, say Base and there are many class derived from
> >it. At a particular point in my application I need the size of the
> >object pointed to by the base pointer. Eg

>
> You should write your code so that it uses only the declared public
> interface of your classes. The size of the object typically is not,
> and should not be, anything for foreign code to depend upon.


Thanks David and Victor for u r Suggestions. The problem is
that i can not demand other module devlopers to implement a getSize()
in their module just to return the sizeof their class; which is of no
use to their objects and as i am implementing the memory management
module, memory management should be handeled by my module, and it
should be insulated, from other modules and should not be dependent on
other. That is what a good design says.
Can you suggest some other solution.

Gopal
Lambent Technologies
 
Reply With Quote
 
Dave Moore
Guest
Posts: n/a
 
      06-02-2004
(E-Mail Removed) (Gopal-M) wrote in message news:<(E-Mail Removed). com>...
> David Harmon <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> > On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, (E-Mail Removed)
> > (Gopal-M) wrote,
> > >I have a Base class, say Base and there are many class derived from
> > >it. At a particular point in my application I need the size of the
> > >object pointed to by the base pointer. Eg

> >
> > You should write your code so that it uses only the declared public
> > interface of your classes. The size of the object typically is not,
> > and should not be, anything for foreign code to depend upon.

>
> Thanks David and Victor for u r Suggestions. The problem is
> that i can not demand other module devlopers to implement a getSize()
> in their module just to return the sizeof their class; which is of no
> use to their objects and as i am implementing the memory management
> module, memory management should be handeled by my module, and it
> should be insulated, from other modules and should not be dependent on
> other. That is what a good design says.
> Can you suggest some other solution.
>
> Gopal
> Lambent Technologies


Hmm .. well, you will probably have to resort to RTTI (which will
likely be exceedingly ugly) to solve the problem in the way you
describe. However, I find it hard to believe that there is not a
better solution on the design end. Actually, I think that making
getSize() a virtual function of the base class and requiring derived
classes to implement it may be reasonable in your case, if in fact you
*really* need to know the sizes of the derived objects to implement
your memory manager.
The point is that such knowledge of the size of derived classes is
generally not necessary in a well-designed application. In most cases
I am familiar with, only pointers to the base-class are stored .. so
pointers to the base class are all you need to know the size of. The
only exception I can think of right now might be by-value copying,
where you want to copy all of the information in the derived type into
a new object, but then you are going to have to downcast anyway. Of
course, once you have done the downcast (correctly) you will be able
to use sizeof(derived_type) in the subsequent code.

Basically what I am saying is that without a more clear-cut
description of your problem, it is hard to suggest any solutions. Why
don't you give us a streamlined example of real (i.e. compilable and
tested) code that fails when you don't know the size of the derived
type?

Dave Moore
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting derived class pointer to private base class pointer subramanian100in@yahoo.com, India C++ 8 08-18-2010 10:54 AM
Cast a pointer to array to base class pointer to array Hansen C++ 3 04-24-2010 03:30 PM
Accessing Base class function using a pointer to a derived class Abhijit Deshpande C++ 7 07-16-2008 08:27 PM
Pointer to class equals pointer to base class Alberto Luaces C++ 6 06-19-2006 06:16 PM
[RTTI] cast base class pointer to <templated> derived class pointer tirath C++ 3 10-12-2003 01:44 PM



Advertisments