Gopi Krishna Komanduri <[EMAIL PROTECTED]> wrote:
> Consider a following snippet

You should really include relevant headers and namespacing.

>   class exm
>   {
>   int *i;
>   public:

You could have just made it a struct.

>   exm()
>   {
>   }
>   };
>   void main()

This is the wrong signature for main. Use...

  int main()

>   {
>   eam obj;
>   cout<<sizeof(obj);  // o/p will be 4 bytes (ofcourse
> according to compiler. Mine is MSVC)

Just say... output is 4 on my machine.

That way, you're not suggesting that it has to 4 bytes
on everyone else's machine.

>   };
>   similarly..
>     I understood that this pointer will be assigned with the
> address of the object which comes as parameter in copy
> constructor.

Yes, but the 'this' pointer is not stored in the object.

> But to assign , "this" pointer has to exist. That means, it
> must be declared (probably hidden).

What would be the point?! If you can't find the object, then
how will it help to know that the object has a pointer to itself
inside of it?!

It's like hidding the map to a treasure chest inside the treasure
chest that you've hidden! If you can't find the treasure chest,
then what good is it to know that there's a map _in_ the treasure
chest? And if you _can_ find the treasure chest then, again, what
was the point of putting the map in the treasure chest!

> So the size of the class should carry the size of this
> hidden pointer also.

No.

> Please correct me. I understood that I am in wrong way.
> But Didn't understand where I missed the track.

What you're really missing is that the language can't have
zero sized objects. Why? Because the language guarantees
(amongst other things):

  1) arrays are contiguous, i.e. elements are laid end to end
       with no spacing between them; and

  2) pointers to different objects will not compare equal.

Now, imagine you declare an array of objects that have
all have zero size. How big is the array? What is the
address of each different element?

Further, how does a member function (or any other piece of
code for that matter) differentiate which element in the
array you're looking at?

To cope with this, what a C++ compiler actually does with
an empty class is to automatically assign padding space in
such classes. The padding doesn't contain any information,
it is simply padding.

The 'this' pointer _is_ hidden, but it's a hidden _parameter_,
not a hidden class member. As a parameter, it's supplied
whenever a member function is invoked against an object.
Because the location of an object has to be known before
you can apply a member function, the address of the object
is silently passed when the function is called. The padding
space of empty classes remains incidental.

-- 
Peter

Reply via email to